• asp.net 生成图片验证码


     1 protected void Page_Load(object sender, EventArgs e)
     2     {
     3         string tmp = RndNum(4);
     4         HttpCookie a = new HttpCookie("ImageV", tmp);
     5         Response.Cookies.Add(a);
     6         this.ValidateCode(tmp); 
     7     }
     8     private void ValidateCode(string VNum)
     9     {
    10         Bitmap Img = null;
    11         Graphics g = null;
    12         MemoryStream ms = null;
    13 
    14         int gheight = VNum.Length * 12;
    15         Img = new Bitmap(gheight, 25);
    16         g = Graphics.FromImage(Img);
    17         //背景颜色
    18         g.Clear(Color.LightSteelBlue);
    19         //文字字体
    20         Font f = new Font("Arial Black"10);
    21         //文字颜色
    22         SolidBrush s = new SolidBrush(Color.RoyalBlue);
    23         g.DrawString(VNum, f, s, 33);
    24         ms = new MemoryStream();
    25         Img.Save(ms, ImageFormat.Jpeg);
    26         Response.ClearContent();
    27         Response.ContentType = "images/Jpeg";
    28         Response.BinaryWrite(ms.ToArray());
    29         g.Dispose();
    30         Img.Dispose();
    31         Response.End();
    32     }
    33 
    34     private string RndNum(int VcodeNum)
    35     {
    36         string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
    37         ",q,r,s,t,u,v,w,x,y,z";
    38         string[] VcArray = Vchar.Split(new Char[] { ',' });
    39         string VNum = "";
    40         int temp = -1;
    41 
    42         Random rand = new Random();
    43 
    44         for (int i = 1; i < VcodeNum + 1; i++)
    45         {
    46             if (temp != -1)
    47             {
    48                 rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
    49             }
    50 
    51             int t = rand.Next(35);
    52             if (temp != -1 && temp == t)
    53             {
    54                 return RndNum(VcodeNum);
    55             }
    56             temp = t;
    57             VNum += VcArray[t];
    58         }
    59         return VNum;
    60     }

    使用方法:
    在需要它的页面html里添加
     <img src="identifyingcode.aspx" /> 

      HttpCookieCollection cookies = Request.Cookies;
      string tmp = cookies["ImageV"].Value;
      然后比tmp与获取的较验证码文本框中的值是否相同

  • 相关阅读:
    虚拟机中安装vmware tools 到 Debian 时出现 找不到kernel headers的提示
    中小企业信息安全:基本原则
    关于开源的一些注意事项
    创建Odoo8数据库时的“new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)“问题
    debian8安装Odoo中的Barcode Scanner Hardware Driver模块时,提示没有evdev
    vim /vi中对字符串的查找并替换
    解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!
    debian命令行删除postgresql数据库
    liunx修改字体为宋体
    OpenERP|odoo Web开发
  • 原文地址:https://www.cnblogs.com/cxy521/p/1048822.html
Copyright © 2020-2023  润新知