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