Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14
15
16public partial class ValidateCode : System.Web.UI.Page
17{
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 //调用方法将验证码生成图片
21 this.CreateCheckCodeImage(GenerateCheckCode());
22 }
23 /**//// <summary>
24 /// 产生五位的随机字符串
25 /// </summary>
26 /// <returns></returns>
27 private string GenerateCheckCode()
28 {
29 int number;
30 char code;
31 string checkCode = String.Empty;
32
33 System.Random random = new Random();
34
35 for (int i = 0; i < 4; i++)
36 {
37 number = random.Next();
38
39 if (number % 2 == 0)
40 code = (char)('0' + (char)(number % 10));
41 else
42 code = (char)('A' + (char)(number % 26));
43
44 checkCode += code.ToString();
45 }
46
47 //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
48 Session["CheckCode"] = checkCode;//用于客户端校验码比较
49
50 return checkCode;
51 }
52 /**//// <summary>
53 /// //将验证码生成图片显示
54 /// </summary>
55 /// <param name="checkCode"></param>
56 private void CreateCheckCodeImage(string checkCode)
57 {
58 if (checkCode == null || checkCode.Trim() == String.Empty)
59 return;
60
61 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
62 Graphics g = Graphics.FromImage(image);
63
64 try
65 {
66 //生成随机生成器
67 Random random = new Random();
68
69 //清空图片背景色
70 g.Clear(Color.White);
71
72 //画图片的背景噪音线
73 for (int i = 0; i < 25; i++)
74 {
75 int x1 = random.Next(image.Width);
76 int x2 = random.Next(image.Width);
77 int y1 = random.Next(image.Height);
78 int y2 = random.Next(image.Height);
79
80 g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
81 }
82
83 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
84 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);
85 g.DrawString(checkCode, font, brush, 2, 2);
86
87 //画图片的前景噪音点
88 for (int i = 0; i < 100; i++)
89 {
90 int x = random.Next(image.Width);
91 int y = random.Next(image.Height);
92
93 image.SetPixel(x, y, Color.FromArgb(random.Next()));
94 }
95
96 //画图片的边框线
97 g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);
98
99 System.IO.MemoryStream ms = new System.IO.MemoryStream();
100 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
101 Response.ClearContent();
102 Response.ContentType = "image/Gif";
103 Response.BinaryWrite(ms.ToArray());
104 }
105 finally
106 {
107 g.Dispose();
108 image.Dispose();
109 }
110 }
111}
112
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14
15
16public partial class ValidateCode : System.Web.UI.Page
17{
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 //调用方法将验证码生成图片
21 this.CreateCheckCodeImage(GenerateCheckCode());
22 }
23 /**//// <summary>
24 /// 产生五位的随机字符串
25 /// </summary>
26 /// <returns></returns>
27 private string GenerateCheckCode()
28 {
29 int number;
30 char code;
31 string checkCode = String.Empty;
32
33 System.Random random = new Random();
34
35 for (int i = 0; i < 4; i++)
36 {
37 number = random.Next();
38
39 if (number % 2 == 0)
40 code = (char)('0' + (char)(number % 10));
41 else
42 code = (char)('A' + (char)(number % 26));
43
44 checkCode += code.ToString();
45 }
46
47 //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
48 Session["CheckCode"] = checkCode;//用于客户端校验码比较
49
50 return checkCode;
51 }
52 /**//// <summary>
53 /// //将验证码生成图片显示
54 /// </summary>
55 /// <param name="checkCode"></param>
56 private void CreateCheckCodeImage(string checkCode)
57 {
58 if (checkCode == null || checkCode.Trim() == String.Empty)
59 return;
60
61 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
62 Graphics g = Graphics.FromImage(image);
63
64 try
65 {
66 //生成随机生成器
67 Random random = new Random();
68
69 //清空图片背景色
70 g.Clear(Color.White);
71
72 //画图片的背景噪音线
73 for (int i = 0; i < 25; i++)
74 {
75 int x1 = random.Next(image.Width);
76 int x2 = random.Next(image.Width);
77 int y1 = random.Next(image.Height);
78 int y2 = random.Next(image.Height);
79
80 g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
81 }
82
83 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
84 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);
85 g.DrawString(checkCode, font, brush, 2, 2);
86
87 //画图片的前景噪音点
88 for (int i = 0; i < 100; i++)
89 {
90 int x = random.Next(image.Width);
91 int y = random.Next(image.Height);
92
93 image.SetPixel(x, y, Color.FromArgb(random.Next()));
94 }
95
96 //画图片的边框线
97 g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);
98
99 System.IO.MemoryStream ms = new System.IO.MemoryStream();
100 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
101 Response.ClearContent();
102 Response.ContentType = "image/Gif";
103 Response.BinaryWrite(ms.ToArray());
104 }
105 finally
106 {
107 g.Dispose();
108 image.Dispose();
109 }
110 }
111}
112