public class ValidateCode : System.Web.UI.Page
2 {
3 private void Page_Load(object sender, System.EventArgs e)
4 {
5 this.CreateCheckCodeImage(GenerateCheckCode());
6 }
7
8 web 窗体设计器生成的代码#region web 窗体设计器生成的代码
9 override protected void OnInit(EventArgs e)
10 {
11 //
12 // CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
13 //
14 InitializeComponent();
15 base.OnInit(e);
16 }
17
18 /**//// <summary>
19 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
20 /// 此方法的内容。
21 /// </summary>
22 private void InitializeComponent()
23 {
24 this.Load += new System.EventHandler(this.Page_Load);
25 }
26 #endregion
27
28 private string GenerateCheckCode()
29 {
30 int number;
31 char code;
32 string checkCode = String.Empty;
33
34 System.Random random = new Random();
35
36 for(int i=0; i<5; i++)
37 {
38 number = random.Next();
39
40 if(number % 2 == 0)
41 code = (char)('0' + (char)(number % 10));
42 else
43 code = (char)('A' + (char)(number % 26));
44
45 checkCode += code.ToString();
46 }
47
48 Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
49
50 return checkCode;
51 }
52
53 private void CreateCheckCodeImage(string checkCode)
54 {
55 if(checkCode == null || checkCode.Trim() == String.Empty)
56 return;
57
58 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
59 Graphics g = Graphics.FromImage(image);
60
61 try
62 {
63 //生成随机生成器
64 Random random = new Random();
65
66 //清空图片背景色
67 g.Clear(Color.White);
68
69 //画图片的背景噪音线
70 for(int i=0; i<25; i++)
71 {
72 int x1 = random.Next(image.Width);
73 int x2 = random.Next(image.Width);
74 int y1 = random.Next(image.Height);
75 int y2 = random.Next(image.Height);
76
77 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
78 }
79
80 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
81 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);
82 g.DrawString(checkCode, font, brush, 2, 2);
83
84 //画图片的前景噪音点
85 for(int i=0; i<100; i++)
86 {
87 int x = random.Next(image.Width);
88 int y = random.Next(image.Height);
89
90 image.SetPixel(x, y, Color.FromArgb(random.Next()));
91 }
92
93 //画图片的边框线
94 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
95
96 System.IO.MemoryStream ms = new System.IO.MemoryStream();
97 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
98 Response.ClearContent();
99 Response.ContentType = "image/Gif";
100 Response.BinaryWrite(ms.ToArray());
101 }
102 finally
103 {
104 g.Dispose();
105 image.Dispose();
106 }
107 }
2 {
3 private void Page_Load(object sender, System.EventArgs e)
4 {
5 this.CreateCheckCodeImage(GenerateCheckCode());
6 }
7
8 web 窗体设计器生成的代码#region web 窗体设计器生成的代码
9 override protected void OnInit(EventArgs e)
10 {
11 //
12 // CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
13 //
14 InitializeComponent();
15 base.OnInit(e);
16 }
17
18 /**//// <summary>
19 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
20 /// 此方法的内容。
21 /// </summary>
22 private void InitializeComponent()
23 {
24 this.Load += new System.EventHandler(this.Page_Load);
25 }
26 #endregion
27
28 private string GenerateCheckCode()
29 {
30 int number;
31 char code;
32 string checkCode = String.Empty;
33
34 System.Random random = new Random();
35
36 for(int i=0; i<5; i++)
37 {
38 number = random.Next();
39
40 if(number % 2 == 0)
41 code = (char)('0' + (char)(number % 10));
42 else
43 code = (char)('A' + (char)(number % 26));
44
45 checkCode += code.ToString();
46 }
47
48 Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
49
50 return checkCode;
51 }
52
53 private void CreateCheckCodeImage(string checkCode)
54 {
55 if(checkCode == null || checkCode.Trim() == String.Empty)
56 return;
57
58 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
59 Graphics g = Graphics.FromImage(image);
60
61 try
62 {
63 //生成随机生成器
64 Random random = new Random();
65
66 //清空图片背景色
67 g.Clear(Color.White);
68
69 //画图片的背景噪音线
70 for(int i=0; i<25; i++)
71 {
72 int x1 = random.Next(image.Width);
73 int x2 = random.Next(image.Width);
74 int y1 = random.Next(image.Height);
75 int y2 = random.Next(image.Height);
76
77 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
78 }
79
80 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
81 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);
82 g.DrawString(checkCode, font, brush, 2, 2);
83
84 //画图片的前景噪音点
85 for(int i=0; i<100; i++)
86 {
87 int x = random.Next(image.Width);
88 int y = random.Next(image.Height);
89
90 image.SetPixel(x, y, Color.FromArgb(random.Next()));
91 }
92
93 //画图片的边框线
94 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
95
96 System.IO.MemoryStream ms = new System.IO.MemoryStream();
97 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
98 Response.ClearContent();
99 Response.ContentType = "image/Gif";
100 Response.BinaryWrite(ms.ToArray());
101 }
102 finally
103 {
104 g.Dispose();
105 image.Dispose();
106 }
107 }