生成验证码的页面:Captcha.aspx
调用验证码的页面:register.aspx
源码下载:下载
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Captcha.aspx.cs" Inherits="Captcha" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12
13 </div>
14 </form>
15</body>
16</html>
17===========================
18Captcha.aspx.cs
19
20using System;
21using System.Data;
22using System.Configuration;
23using System.Collections;
24using System.Web;
25using System.Web.Security;
26using System.Web.UI;
27using System.Web.UI.WebControls;
28using System.Web.UI.WebControls.WebParts;
29using System.Web.UI.HtmlControls;
30using System.Drawing.Drawing2D;
31using System.Drawing;
32using System.IO;
33using System.Drawing.Imaging;
34
35public partial class Captcha : System.Web.UI.Page
36{
37 private Bitmap validateimage;
38 private Graphics g;
39 public void Page_Load(object Sender, EventArgs e)
40 {
41 Response.BufferOutput = true; //特别注意
42 Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
43 Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
44 Response.AppendHeader("Pragma", "No-Cache"); //特别注意
45 string VNum = MakeValidateCode();
46 Session["VNum"] = VNum.ToUpper();//取得验证码,以便后来验证 大写
47 ValidateCode(VNum.ToUpper());
48 }
49 public void ValidateCode(string VNum)
50 {
51 validateimage = new Bitmap(100, 20, PixelFormat.Format24bppRgb);
52 g = Graphics.FromImage(validateimage);
53 g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200);
54 g.DrawString(VNum, new Font("arial", 12), new SolidBrush(Color.Red), new PointF(6, 0));
55 g.Save();
56 MemoryStream ms = new MemoryStream();
57 validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
58 Response.ClearContent();
59 Response.ContentType = "image/bmp";
60 Response.BinaryWrite(ms.ToArray());
61 Response.End();
62 }
63
64 string MakeValidateCode()
65 {
//这里可以改成你想设置的验证码字符集
66 //char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
67 char[] s = new char[] { '张', '王', '李', '赵', '钱', '孙', '1', '7', '8', '9'};
68 string num = "";
69 Random r = new Random();
70 for (int i = 0; i < 5; i++)
71 {
72 num += s[r.Next(0, s.Length)].ToString();
73 }
74 return num;
75 }
76
77
78}
79
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12
13 </div>
14 </form>
15</body>
16</html>
17===========================
18Captcha.aspx.cs
19
20using System;
21using System.Data;
22using System.Configuration;
23using System.Collections;
24using System.Web;
25using System.Web.Security;
26using System.Web.UI;
27using System.Web.UI.WebControls;
28using System.Web.UI.WebControls.WebParts;
29using System.Web.UI.HtmlControls;
30using System.Drawing.Drawing2D;
31using System.Drawing;
32using System.IO;
33using System.Drawing.Imaging;
34
35public partial class Captcha : System.Web.UI.Page
36{
37 private Bitmap validateimage;
38 private Graphics g;
39 public void Page_Load(object Sender, EventArgs e)
40 {
41 Response.BufferOutput = true; //特别注意
42 Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
43 Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
44 Response.AppendHeader("Pragma", "No-Cache"); //特别注意
45 string VNum = MakeValidateCode();
46 Session["VNum"] = VNum.ToUpper();//取得验证码,以便后来验证 大写
47 ValidateCode(VNum.ToUpper());
48 }
49 public void ValidateCode(string VNum)
50 {
51 validateimage = new Bitmap(100, 20, PixelFormat.Format24bppRgb);
52 g = Graphics.FromImage(validateimage);
53 g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200);
54 g.DrawString(VNum, new Font("arial", 12), new SolidBrush(Color.Red), new PointF(6, 0));
55 g.Save();
56 MemoryStream ms = new MemoryStream();
57 validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
58 Response.ClearContent();
59 Response.ContentType = "image/bmp";
60 Response.BinaryWrite(ms.ToArray());
61 Response.End();
62 }
63
64 string MakeValidateCode()
65 {
//这里可以改成你想设置的验证码字符集
66 //char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
67 char[] s = new char[] { '张', '王', '李', '赵', '钱', '孙', '1', '7', '8', '9'};
68 string num = "";
69 Random r = new Random();
70 for (int i = 0; i < 5; i++)
71 {
72 num += s[r.Next(0, s.Length)].ToString();
73 }
74 return num;
75 }
76
77
78}
79
调用验证码的页面:register.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head id="Head1" runat="server">
7 <title>会员注册</title>
8 </head>
9<body>
10 <form id="form1" runat="server">
11 <div class="middle" style="height: 1px" >
12 <div >
13 <asp:Label ID="UserName" runat="server" Text="用户名:" ></asp:Label>
14 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
15 </div>
16 <div >
17 <asp:Label ID="UserPsw" runat="server" Text="密码:" ></asp:Label>
18 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
19 <div >
20 <asp:Label ID="Label2" runat="server" Text="确认密码:" ></asp:Label>
21 <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></div>
22 <div >
23 <asp:Label ID="Question" runat="server" Text="找回密码问题:"></asp:Label>
24 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></div>
25 <div>
26 <asp:Label ID="Answer" runat="server" Text="找回密码答案:"></asp:Label>
27 <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></div>
28 <div >
29 <asp:Label ID="Email" runat="server" Text="电子邮件:"></asp:Label>
30 <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></div>
31 <div >
32 <asp:Label ID="Label1" runat="server" Text="验证码:"></asp:Label>
33 <asp:TextBox ID="CheckCode" runat="server" Width="85px"></asp:TextBox>
34 <asp:Image id="Image1" runat="server" style="display:inline;" ImageUrl="Captcha.aspx"></asp:Image>
35 <asp:LinkButton ID="LinkButton1" runat="server">看不清楚,再换一张</asp:LinkButton></div>
36 <div>
37 <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" /></div>
38 <div class="text">
39 <asp:Label ID="Message" runat="server" ></asp:Label></div>
40 </div>
41 </form>
42</body>
43
44</html>
45
46=============================
47Register.aspx.cs
48
49using System;
50using System.Data;
51using System.Configuration;
52using System.Collections;
53using System.Web;
54using System.Web.Security;
55using System.Web.UI;
56using System.Web.UI.WebControls;
57using System.Web.UI.WebControls.WebParts;
58using System.Web.UI.HtmlControls;
59
60
61public partial class register : Page
62 {
63 protected void Page_Load(object sender, EventArgs e)
64 {
65
66 }
67 protected void Button1_Click(object sender, EventArgs e)
68 {
69 string checkcode = CheckCode.Text;
70 //Response.Write(Session["VNum"]);
71 if (Session["VNum"].ToString() == null || checkcode.ToUpper() == Session["VNum"].ToString())//注意Session["VNum"].ToString(),必须加上ToString(),因//为Session["VNum"]是对象。
72 Response.Redirect("default.aspx");
73 else
74 Message.Text = "验证码错误或为空!";
75 }
76 }
77
78
79
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head id="Head1" runat="server">
7 <title>会员注册</title>
8 </head>
9<body>
10 <form id="form1" runat="server">
11 <div class="middle" style="height: 1px" >
12 <div >
13 <asp:Label ID="UserName" runat="server" Text="用户名:" ></asp:Label>
14 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
15 </div>
16 <div >
17 <asp:Label ID="UserPsw" runat="server" Text="密码:" ></asp:Label>
18 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
19 <div >
20 <asp:Label ID="Label2" runat="server" Text="确认密码:" ></asp:Label>
21 <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></div>
22 <div >
23 <asp:Label ID="Question" runat="server" Text="找回密码问题:"></asp:Label>
24 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></div>
25 <div>
26 <asp:Label ID="Answer" runat="server" Text="找回密码答案:"></asp:Label>
27 <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></div>
28 <div >
29 <asp:Label ID="Email" runat="server" Text="电子邮件:"></asp:Label>
30 <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></div>
31 <div >
32 <asp:Label ID="Label1" runat="server" Text="验证码:"></asp:Label>
33 <asp:TextBox ID="CheckCode" runat="server" Width="85px"></asp:TextBox>
34 <asp:Image id="Image1" runat="server" style="display:inline;" ImageUrl="Captcha.aspx"></asp:Image>
35 <asp:LinkButton ID="LinkButton1" runat="server">看不清楚,再换一张</asp:LinkButton></div>
36 <div>
37 <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" /></div>
38 <div class="text">
39 <asp:Label ID="Message" runat="server" ></asp:Label></div>
40 </div>
41 </form>
42</body>
43
44</html>
45
46=============================
47Register.aspx.cs
48
49using System;
50using System.Data;
51using System.Configuration;
52using System.Collections;
53using System.Web;
54using System.Web.Security;
55using System.Web.UI;
56using System.Web.UI.WebControls;
57using System.Web.UI.WebControls.WebParts;
58using System.Web.UI.HtmlControls;
59
60
61public partial class register : Page
62 {
63 protected void Page_Load(object sender, EventArgs e)
64 {
65
66 }
67 protected void Button1_Click(object sender, EventArgs e)
68 {
69 string checkcode = CheckCode.Text;
70 //Response.Write(Session["VNum"]);
71 if (Session["VNum"].ToString() == null || checkcode.ToUpper() == Session["VNum"].ToString())//注意Session["VNum"].ToString(),必须加上ToString(),因//为Session["VNum"]是对象。
72 Response.Redirect("default.aspx");
73 else
74 Message.Text = "验证码错误或为空!";
75 }
76 }
77
78
79
源码下载:下载