(ASP.NET 2.0)关于登录界面的验证码(留着以后备用)

我们都知道,在进入一些网站的登录或注册页面时,要完成登录或注册,都会要我们把一个图片里的数字或字母输入到对应的文本框中,如果正确就可以完成相关操作,否则提示验证码错误。验证码的作用主要有:防止暴力破译密码(即用试探法登录);减少对服务器的访问负担。

基本的制作方法是:首先生成n位(本例中采用5位)字符串,写入图片控件中,然后画x条(本例中采用10条)左右的直线(位置随机),再画y个(本例中采用100个)左右的点(位置随机),这样就做好了,有些论坛验证码还会有颜色。

首先引入using System.Drawing;的命名空间;

再写一个随机字符串生成方法GenerateCheckCode(),函数体如下

 1         int number;
 2         char code;
 3         string checkCode = String.Empty;
 4 
 5         System.Random random = new Random();
 6 
 7         for (int i = 0; i < 6; i++)
 8         {
 9             number = random.Next();
10 
11             if (number % 2 == 0)
12                 code = (char)('0' + (char)(number % 10));
13             else
14                 code = (char)('A' + (char)(number % 26));
15 
16             checkCode += code.ToString();
17         }
18         Session["CheckCode"] = checkCode;
19 
20         //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
21 
22         return checkCode;

这里使用了一个Session对象来储存生成的字符串;

然后就是开始制作这张验证图片了,写一个图片生成方法CreateCheckCodeImage(string checkCode),其方法体如下

 1         if (checkCode == null || checkCode.Trim() == String.Empty)
 2             return;
 3 
 4         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
 5         Graphics g = Graphics.FromImage(image);
 6 
 7         try
 8         {
 9             //生成随机生成器
10             Random random = new Random();
11 
12             //清空图片背景色
13             g.Clear(Color.White);
14 
15             //画图片的背景噪音线
16             for (int i = 0; i < 25; i++)
17             {
18                 int x1 = random.Next(image.Width);
19                 int x2 = random.Next(image.Width);
20                 int y1 = random.Next(image.Height);
21                 int y2 = random.Next(image.Height);
22 
23                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
24             }
25 
26             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
27             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
28             g.DrawString(checkCode, font, brush, 2, 2);
29 
30             //画图片的前景噪音点
31             for (int i = 0; i < 100; i++)
32             {
33                 int x = random.Next(image.Width);
34                 int y = random.Next(image.Height);
35 
36                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
37             }
38 
39             //画图片的边框线
40             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
41 
42             System.IO.MemoryStream ms = new System.IO.MemoryStream();
43             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
44             Response.ClearContent();
45             Response.ContentType = "image/Gif";
46             Response.BinaryWrite(ms.ToArray());
47         }
48         finally
49         {
50             g.Dispose();
51             image.Dispose();
52         }


最后在Page_Load事件中添加一句话:this.CreateCheckCodeImage(GenerateCheckCode());

OK,完成制作。

原文地址:https://www.cnblogs.com/jijiexuanfeng/p/2999038.html