zoukankan      html  css  js  c++  java
  • 如何写一个验证码

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.IO;
    using System.Text;

    namespace 验证码
    {
        public partial class pic : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                System.Drawing.Image img = new Bitmap(150, 50);

                Graphics g = Graphics.FromImage(img);
                this.AddPoint(img, 100);
                string code = this.GeneralCode();
                Font font1=new Font("宋体",30,FontStyle.Italic);
                g.DrawString(code, font1, Brushes.Red, 0, 0);
                this.Response.Clear();
                MemoryStream ms = new MemoryStream();//创建一个缓存流
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//将此图像以指定的格式保存到指定的流中
                this.Response.BinaryWrite(ms.ToArray());
                this.Response.Flush();//输出流
                this.Response.End();//关闭流

              
            }
            private void AddPoint(System.Drawing.Image img, int nums)//加噪点
            {
                Bitmap b = img as Bitmap;
                Random ran = new Random();
                for (int i = 0; i < nums; i++)
                {
                    b.SetPixel(ran.Next(0, img.Width), ran.Next(0, img.Height), Color.White);
                       
                }
            }
            //随即生成数字
            private string GeneralCode()
            {
                Random ran = new Random(DateTime.Now.Millisecond);
                StringBuilder sb = new StringBuilder(6);
                for (int i = 0; i < 6; i++)
                {
                    sb.Append(ran.Next(0, 9));

     
                }
                return sb.ToString();
            }
                
        }
    }

  • 相关阅读:
    Mac使用pip安装Tensorflow
    php之curl get post curl_multi_exec 请求用法
    重新捡起的知识-字节(Byte)、比特(bit)-计算机常识
    Mac打不开Wireshark dyld: Library not loaded: /usr/X11/lib/libcairo.2.dylib
    ViewController生命周期
    转 try catch finally
    VPS centOS搭建gitlab小结
    UIButton小节
    python 统计单词个数,并按个数与字母排序
    GCP 谷歌云平台申请教程
  • 原文地址:https://www.cnblogs.com/poiu/p/2834700.html
Copyright © 2011-2022 走看看