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