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