• 验证码


    1 protected void Page_Load(object sender, EventArgs e)
    2     {
    3 Bitmap bitmap = CreateBitmap(GetRandomDigit(2)+GetRandomNumbers(2));
    4         bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
    5         bitmap.Dispose();
    6     }
    7     #region 数字验证
    8     public string GetRandomDigit(int len)
    9     {
    10         string str = "1234567890";
    11         string strResult = "";
    12         Random random = new Random(unchecked((int)DateTime.Now.Ticks));
    13         for (int i = 0; i < len; i++)
    14         {
    15             strResult +=str[random.Next(0,10)].ToString();
    16         }
    17         return strResult;
    18     }
    19     #endregion
    20
    21
    22     #region 字母验证
    23     public string GetRandomNumbers(int len)
    24     {
    25         string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    26         String strResult = "";
    27         Random random = new Random();
    28         for (int i = 0; i < len; i++)
    29         {
    30             strResult += str[random.Next(0, 10)].ToString();
    31         }
    32         return strResult;
    33     }
    34
    35     #endregion
    36
    37
    38     public Bitmap CreateBitmap(string check)
    39     {
    40         int width = (int)(check.Length * 12);
    41         Bitmap bitmap = new Bitmap(width, 20);
    42         Graphics graphics = Graphics.FromImage(bitmap);
    43         graphics.Clear(Color.White);
    44         Color[] c ={ Color.Black,Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan,Color.Purple};
    45         string[] font = { "Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体"};
    46         Random random = new Random();
    47         for (int i = 0; i < 30; i++)
    48         {
    49             int x1 = random.Next(bitmap.Width);
    50             int x2 = random.Next(bitmap.Width);
    51             int y1 = random.Next(bitmap.Height);
    52             int y2 = random.Next(bitmap.Height);
    53             graphics.DrawLine(new Pen(Color.LightGray, 1), new Point(x1, y1), new Point(x2, y2));
    54
    55         }
    56         for (int i = 0; i < check.Length; i++)
    57         {
    58             int cIndex = random.Next(c.Length-1);
    59             int fIndex = random.Next(font.Length);
    60             Font f = new Font(font[fIndex].ToString(),10,  FontStyle.Regular);
    61             Brush b = new SolidBrush(c[cIndex]);
    62             int ii = 4;
    63             if ((i +1)% 2 == 0)
    64             {
    65                 ii = 2;
    66             }
    67             graphics.DrawString(check.Substring(i, 1), f, b, new PointF(3 + (i * 12 ), ii));
    68         }
    69         graphics.Flush();
    70       
    71         return bitmap;
    72     }
  • 相关阅读:
    登陆中发现css页面不见了,变成了光秃秃的输入框问题
    servlet基础一
    解决问题:Error:(5, 59) java: 程序包javax.servlet.http不存在
    静态方法的引用
    登录功能之添加验证码(包含切换验证码)
    生成图片验证码(VerifyCode)
    idea编辑器中批量完成注释及批量取消注释
    jdbc连接测试
    利用IO流复制图片
    js 判空
  • 原文地址:https://www.cnblogs.com/caishuowen/p/2050284.html
Copyright © 2020-2023  润新知