ASP.NET with C#生成验证码的过程
生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。
具体生成验证码的代码如下,在生成随机字符串的同时会将字符串设置到一个Session["ValidateCode"] 中,实用的时候只要得到用户返回值和Session值比较就可以得出填入的验证码是否一致了。
1
using
System;
2 using System.IO;
3 using System.Web.UI;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.Drawing.Drawing2D;
7
8 namespace Web.Common
9 {
10 /// <summary>
11 /// validate 的摘要说明。
12 /// </summary>
13 public class Validate: System.Web.UI.Page
14 {
15 private void Page_Load(object sender, EventArgs e)
16 {
17 string strValidateCode = ValidateCode(6);//取得随机字符串,并设置Session值
18 DrawValidateCode(strValidateCode,50,100);//绘图
19 }
20
21 //绘图
22 private void DrawValidateCode(string strValidateCode,int intFgNoise,int intBgNoise)
23 {
24 if(strValidateCode == null || strValidateCode.Trim() == String.Empty)
25 {
26 return;
27 }
28 else
29 {
30 //建立一个位图文件 确立长宽
31 Bitmap bmpImage = new Bitmap((int)Math.Ceiling((strValidateCode.Length * 12.5)), 22);
32 Graphics grpGraphics = Graphics.FromImage(bmpImage);
33
34 try
35 {
36 //生成随机生成器
37 Random rndRandom = new Random();
38
39 //清空图片背景色
40 grpGraphics.Clear(Color.White);
41
42 //画图片的背景噪音线
43 for(int i=0; i<intBgNoise; i++)
44 {
45 int int_x1 = rndRandom.Next(bmpImage.Width);
46 int int_x2 = rndRandom.Next(bmpImage.Width);
47 int int_y1 = rndRandom.Next(bmpImage.Height);
48 int int_y2 = rndRandom.Next(bmpImage.Height);
49
50 grpGraphics.DrawLine(new Pen(Color.Silver), int_x1, int_y1, int_x2, int_y2);
51 }
52 //把产生的随机数以字体的形式写入画面
53 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
54 LinearGradientBrush brhBrush = new LinearGradientBrush(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), Color.Blue, Color.DarkRed, 1.2f, true);
55 grpGraphics.DrawString(strValidateCode, font, brhBrush, 2, 2);
56
57 //画图片的前景噪音点
58 for(int i=0; i<intFgNoise; i++)
59 {
60 int int_x = rndRandom.Next(bmpImage.Width);
61 int int_y = rndRandom.Next(bmpImage.Height);
62
63 bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
64 }
65
66 //画图片的边框线
67 grpGraphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bmpImage.Width - 1, bmpImage.Height - 1);
68
69 MemoryStream memsMemoryStream = new MemoryStream();
70 bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
71 Response.ClearContent();
72 Response.ContentType = "image/Gif";
73 Response.BinaryWrite(memsMemoryStream.ToArray());
74 }
75 finally
76 {
77 grpGraphics.Dispose();
78 bmpImage.Dispose();
79 }
80 }
81 }
82
83 //取得随机字符串,并设置Session值
84 private string ValidateCode(int intLength)
85 {
86 int intNumber;
87 char chrCode;
88 string strValidateCode = String.Empty;
89
90 Random rndRandom = new Random();
91
92 for(int i=0;i<intLength;i++)
93 {
94 intNumber = rndRandom.Next();
95 if(intNumber % 2 == 0)
96 {
97 chrCode = (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余
98 }
99 else
100 {
101 chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
102 }
103 strValidateCode += chrCode.ToString();
104 }
105
106 Session["ValidateCode"] = strValidateCode;//设置Session["ValidateCode"]
107 //Response.Cookies.Add(new HttpCookie("strValidateCode",strValidateCode));
108
109 return strValidateCode;
110 }
111
112 Web 窗体设计器生成的代码
132 }
133
134}
135
2 using System.IO;
3 using System.Web.UI;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.Drawing.Drawing2D;
7
8 namespace Web.Common
9 {
10 /// <summary>
11 /// validate 的摘要说明。
12 /// </summary>
13 public class Validate: System.Web.UI.Page
14 {
15 private void Page_Load(object sender, EventArgs e)
16 {
17 string strValidateCode = ValidateCode(6);//取得随机字符串,并设置Session值
18 DrawValidateCode(strValidateCode,50,100);//绘图
19 }
20
21 //绘图
22 private void DrawValidateCode(string strValidateCode,int intFgNoise,int intBgNoise)
23 {
24 if(strValidateCode == null || strValidateCode.Trim() == String.Empty)
25 {
26 return;
27 }
28 else
29 {
30 //建立一个位图文件 确立长宽
31 Bitmap bmpImage = new Bitmap((int)Math.Ceiling((strValidateCode.Length * 12.5)), 22);
32 Graphics grpGraphics = Graphics.FromImage(bmpImage);
33
34 try
35 {
36 //生成随机生成器
37 Random rndRandom = new Random();
38
39 //清空图片背景色
40 grpGraphics.Clear(Color.White);
41
42 //画图片的背景噪音线
43 for(int i=0; i<intBgNoise; i++)
44 {
45 int int_x1 = rndRandom.Next(bmpImage.Width);
46 int int_x2 = rndRandom.Next(bmpImage.Width);
47 int int_y1 = rndRandom.Next(bmpImage.Height);
48 int int_y2 = rndRandom.Next(bmpImage.Height);
49
50 grpGraphics.DrawLine(new Pen(Color.Silver), int_x1, int_y1, int_x2, int_y2);
51 }
52 //把产生的随机数以字体的形式写入画面
53 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
54 LinearGradientBrush brhBrush = new LinearGradientBrush(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), Color.Blue, Color.DarkRed, 1.2f, true);
55 grpGraphics.DrawString(strValidateCode, font, brhBrush, 2, 2);
56
57 //画图片的前景噪音点
58 for(int i=0; i<intFgNoise; i++)
59 {
60 int int_x = rndRandom.Next(bmpImage.Width);
61 int int_y = rndRandom.Next(bmpImage.Height);
62
63 bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
64 }
65
66 //画图片的边框线
67 grpGraphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bmpImage.Width - 1, bmpImage.Height - 1);
68
69 MemoryStream memsMemoryStream = new MemoryStream();
70 bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
71 Response.ClearContent();
72 Response.ContentType = "image/Gif";
73 Response.BinaryWrite(memsMemoryStream.ToArray());
74 }
75 finally
76 {
77 grpGraphics.Dispose();
78 bmpImage.Dispose();
79 }
80 }
81 }
82
83 //取得随机字符串,并设置Session值
84 private string ValidateCode(int intLength)
85 {
86 int intNumber;
87 char chrCode;
88 string strValidateCode = String.Empty;
89
90 Random rndRandom = new Random();
91
92 for(int i=0;i<intLength;i++)
93 {
94 intNumber = rndRandom.Next();
95 if(intNumber % 2 == 0)
96 {
97 chrCode = (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余
98 }
99 else
100 {
101 chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
102 }
103 strValidateCode += chrCode.ToString();
104 }
105
106 Session["ValidateCode"] = strValidateCode;//设置Session["ValidateCode"]
107 //Response.Cookies.Add(new HttpCookie("strValidateCode",strValidateCode));
108
109 return strValidateCode;
110 }
111
112 Web 窗体设计器生成的代码
132 }
133
134}
135