生成登录验证码(asp.net)

第一步:创建一个类CreateImage
 1 public   class  CreateImage
 2 {
 3    internal static void DrawImage()
 4    {
 5        CreateImage img = new CreateImage();
 6        HttpContext.Current.Session["CheckCode"= img.RndNum(4);
 7        img.CreateImages(HttpContext.Current.Session["CheckCode"].ToString());
 8    }

 9
10    /// <summary>
11    /// 生成验证图片
12    /// </summary>
13    /// <param name="checkCode">验证字符</param>

14    private void CreateImages(string checkCode)
15    {
16        int iwidth = (int)(checkCode.Length * 13);
17        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
18        Graphics g = Graphics.FromImage(image);
19        g.Clear(Color.White);
20        //定义颜色
21        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
22        //定义字体            
23        string[] font = "Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
24        Random rand = new Random();
25        //随机输出噪点
26        for (int i = 0; i < 50; i++)
27        {
28            int x = rand.Next(image.Width);
29            int y = rand.Next(image.Height);
30            g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 11);
31        }

32
33        //输出不同字体和颜色的验证码字符
34        for (int i = 0; i < checkCode.Length; i++)
35        {
36            int cindex = rand.Next(7);
37            int findex = rand.Next(5);
38
39            Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
40            Brush b = new System.Drawing.SolidBrush(c[cindex]);
41            int ii = 4;
42            if ((i + 1% 2 == 0)
43            {
44                ii = 2;
45            }

46            g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
47        }

48        //画一个边框
49        g.DrawRectangle(new Pen(Color.Black, 0), 00, image.Width - 1, image.Height - 1);
50
51        //输出到浏览器
52        System.IO.MemoryStream ms = new System.IO.MemoryStream();
53        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
54        HttpContext.Current.Response.ClearContent();
55        //Response.ClearContent();
56        HttpContext.Current.Response.ContentType = "image/Jpeg";
57        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
58        g.Dispose();
59        image.Dispose();
60    }

61
62    /// <summary>
63    /// 生成随机的字母
64    /// </summary>
65    /// <param name="VcodeNum">生成字母的个数</param>
66    /// <returns>string</returns>

67    private string RndNum(int VcodeNum)
68    {
69        string Vchar = "0,1,2,3,4,5,6,7,8,9";
70        string[] VcArray = Vchar.Split(',');
71        string VNum = "";   //由于字符串很短,就不用StringBuilder了
72        int temp = -1;       //记录上次随机数值,尽量避免生产几个一样的随机数
73
74        //采用一个简单的算法以保证生成随机数的不同
75        Random rand = new Random();
76        for (int i = 1; i < VcodeNum + 1; i++)
77        {
78            if (temp != -1)
79            {
80                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
81            }

82            int t = rand.Next(VcArray.Length);
83            if (temp != -1 && temp == t)
84            {
85                return RndNum(VcodeNum);
86            }

87            temp = t;
88            VNum += VcArray[t];
89
90        }

91        return VNum;
92    }

93}
第二步:建立一个页面Image.aspx在Page_Load事件中
 CreateImage.DrawImage();
第三步在调用生成验证码的页面写:
1   < script language = " JavaScript " >
2 <!--
3 var  numkey  =  Math.random();
4 numkey  =  Math.round(numkey * 10000 );
5 document.write( " <img src=/ " Image.aspx ? k = " + numkey + " / "  width=/ " 52 / "  height=/ " 20 / "" );
6 // -->
7 </ script >
8
第四步验证的方法:
1 if  (TbCode.Text.Trim().ToString()  !=  Session[ " CheckCode " ].ToString())
2          {
3            Response.Write("<script language='javascript'>alert('验证码不正确!');</script>");
4            TbCode.Text = "";
5            TbCode.Focus();
6        }

7

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值