Code
1using System;
2using System.Collections;
3using System.Configuration;
4using System.Data;
5using System.Linq;
6using System.Threading;
7using System.Web;
8using System.Web.Security;
9using System.Web.UI;
10using System.Web.UI.HtmlControls;
11using System.Web.UI.WebControls;
12using System.Web.UI.WebControls.WebParts;
13using System.Xml.Linq;
14using System.Drawing;
15using System.Drawing.Imaging;
16using System.Drawing.Drawing2D;
17
18public partial class CheckCode : System.Web.UI.Page
19{
20 private void Page_Load(object sender, System.EventArgs e)
21 {
22 string checkCode = CreateRandomCode(4);
23 Session["CheckCode"] = checkCode;
24 CreateImage(checkCode);
25 }
26 private string CreateRandomCode(int codeCount)
27 {
28 string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
29 string[] allCharArray = allChar.Split(',');
30 string randomCode = "";
31 int temp = -1;
32
33 Random rand = new Random();
34 for (int i = 0; i < codeCount; i++)
35 {
36 if (temp != -1)
37 {
38 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
39 }
40 int t = rand.Next(35);
41 if (temp == t)
42 {
43 return CreateRandomCode(codeCount);
44 }
45 temp = t;
46 randomCode += allCharArray[t];
47 }
48 return randomCode;
49 }
50 private void CreateImage(string checkCode)
51 {
52 int iwidth = (int)(checkCode.Length * 13);
53 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);
54 Graphics g = Graphics.FromImage(image);
55 g.Clear(Color.White);
56 //定义颜色
57 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
58 //定义字体
59 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
60 Random rand = new Random();
61 //随机输出噪点
62 for (int i = 0; i < 50; i++)
63 {
64 int x = rand.Next(image.Width);
65 int y = rand.Next(image.Height);
66 g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
67 }
68
69 //输出不同字体和颜色的验证码字符
70 for (int i = 0; i < checkCode.Length; i++)
71 {
72 int cindex = rand.Next(7);
73 int findex = rand.Next(5);
74
75 Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
76 Brush b = new System.Drawing.SolidBrush(c[cindex]);
77 int ii = 4;
78 if ((i + 1) % 2 == 0)
79 {
80 ii = 2;
81 }
82 g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
83 }
84 //画一个边框
85 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 3);
86
87 //输出到浏览器
88 System.IO.MemoryStream ms = new System.IO.MemoryStream();
89 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
90 HttpContext.Current.Response.ClearContent();
91 //Response.ClearContent();
92 HttpContext.Current.Response.ContentType = "image/Jpeg";
93 HttpContext.Current.Response.BinaryWrite(ms.ToArray());
94 g.Dispose();
95 image.Dispose();
96
97 }
98}
99
100
1using System;
2using System.Collections;
3using System.Configuration;
4using System.Data;
5using System.Linq;
6using System.Threading;
7using System.Web;
8using System.Web.Security;
9using System.Web.UI;
10using System.Web.UI.HtmlControls;
11using System.Web.UI.WebControls;
12using System.Web.UI.WebControls.WebParts;
13using System.Xml.Linq;
14using System.Drawing;
15using System.Drawing.Imaging;
16using System.Drawing.Drawing2D;
17
18public partial class CheckCode : System.Web.UI.Page
19{
20 private void Page_Load(object sender, System.EventArgs e)
21 {
22 string checkCode = CreateRandomCode(4);
23 Session["CheckCode"] = checkCode;
24 CreateImage(checkCode);
25 }
26 private string CreateRandomCode(int codeCount)
27 {
28 string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
29 string[] allCharArray = allChar.Split(',');
30 string randomCode = "";
31 int temp = -1;
32
33 Random rand = new Random();
34 for (int i = 0; i < codeCount; i++)
35 {
36 if (temp != -1)
37 {
38 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
39 }
40 int t = rand.Next(35);
41 if (temp == t)
42 {
43 return CreateRandomCode(codeCount);
44 }
45 temp = t;
46 randomCode += allCharArray[t];
47 }
48 return randomCode;
49 }
50 private void CreateImage(string checkCode)
51 {
52 int iwidth = (int)(checkCode.Length * 13);
53 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);
54 Graphics g = Graphics.FromImage(image);
55 g.Clear(Color.White);
56 //定义颜色
57 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
58 //定义字体
59 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
60 Random rand = new Random();
61 //随机输出噪点
62 for (int i = 0; i < 50; i++)
63 {
64 int x = rand.Next(image.Width);
65 int y = rand.Next(image.Height);
66 g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
67 }
68
69 //输出不同字体和颜色的验证码字符
70 for (int i = 0; i < checkCode.Length; i++)
71 {
72 int cindex = rand.Next(7);
73 int findex = rand.Next(5);
74
75 Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
76 Brush b = new System.Drawing.SolidBrush(c[cindex]);
77 int ii = 4;
78 if ((i + 1) % 2 == 0)
79 {
80 ii = 2;
81 }
82 g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
83 }
84 //画一个边框
85 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 3);
86
87 //输出到浏览器
88 System.IO.MemoryStream ms = new System.IO.MemoryStream();
89 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
90 HttpContext.Current.Response.ClearContent();
91 //Response.ClearContent();
92 HttpContext.Current.Response.ContentType = "image/Jpeg";
93 HttpContext.Current.Response.BinaryWrite(ms.ToArray());
94 g.Dispose();
95 image.Dispose();
96
97 }
98}
99
100
调用形式:
Code
1 <tr><td><strong>请输入验证码:</strong></td>
2 <td colspan="2"><asp:TextBox ID="CheckCode" runat="server" MaxLength ="4" ></asp:TextBox></td>
3 <td colspan="1">
4 <asp:Image Runat="server" ID="ImageCheck" ImageUrl="~/CheckCode.aspx"></asp:Image>
5 </td>
6 </tr>
7 <tr><td colspan="1"></td><td colspan="2"><span style="color:Red">*验证码区别大小写</span></td>
8 <td><asp:LinkButton id="ChangeOther" runat="server" text="看不清换一张" CausesValidation="false"></asp:LinkButton> </td>
9 </tr>
1 <tr><td><strong>请输入验证码:</strong></td>
2 <td colspan="2"><asp:TextBox ID="CheckCode" runat="server" MaxLength ="4" ></asp:TextBox></td>
3 <td colspan="1">
4 <asp:Image Runat="server" ID="ImageCheck" ImageUrl="~/CheckCode.aspx"></asp:Image>
5 </td>
6 </tr>
7 <tr><td colspan="1"></td><td colspan="2"><span style="color:Red">*验证码区别大小写</span></td>
8 <td><asp:LinkButton id="ChangeOther" runat="server" text="看不清换一张" CausesValidation="false"></asp:LinkButton> </td>
9 </tr>