ASP.NET图片验证码的实现

生成图片验证码页面createImg.aspx,验证页面WebForm1.aspx。

CreateImg.aspx页面使用的各个函数如下:

          string getRandomValidate(int len)       得到随机长度为len的字符串

          drawLine(Graphics gfc,Bitmap img)      在图片中画底线

          drawPoint(Bitmap img)                    在图片中画杂点(移动画的杂点挺好不错)

         getImageValidate(string strValue)     使用getRandomValidate函数返回的字符串生成图片

         其具体实现代码如下所示:

 

View Code
  1 using System;
2
3 using System.Collections.Generic;
4
5 using System.Linq;
6
7 using System.Web;
8
9 using System.Web.UI;
10
11 using System.Web.UI.WebControls;
12
13 using System.Drawing;
14
15 using System.IO;
16
17
18
19 namespace WebCA
20
21 {
22
23 public partial class createImg : System.Web.UI.Page
24
25 {
26
27 Random ran = new Random();
28
29 protected void Page_Load(object sender, EventArgs e)
30
31 {
32
33 string str = Session["check"].ToString();
34
35 // Session["check"] = str; //这一步是为了将验证码写入Session,进行验证,不能缺省,也可一使用cookie
36
37 getImageValidate(str);
38
39 }
40
41 //得到随机字符串,长度自己定义
42
43 private string getRandomValidate(int len)
44
45 {
46
47 int num;
48
49 int tem;
50
51 string rtuStr = "";
52
53 for (int i = 0; i < len; i++)
54
55 {
56
57 num = ran.Next();
58
59 /*
60
61 * 这里可以选择生成字符和数字组合的验证码
62
63 */
64
65 tem = num % 10 + '0';//生成数字
66
67 //tem = num % 26 + 'A';//生成字符
68
69 rtuStr += Convert.ToChar(tem).ToString();
70
71 }
72
73 return rtuStr;
74
75 }
76
77 //生成图像
78
79 private void getImageValidate(string strValue)
80
81 {
82
83 //string str = "OO00"; //前两个为字母O,后两个为数字0
84
85 int width = Convert.ToInt32(strValue.Length * 12); //计算图像宽度
86
87 Bitmap img = new Bitmap(width, 23);
88
89 Graphics gfc = Graphics.FromImage(img); //产生Graphics对象,进行画图
90
91 gfc.Clear(Color.White);
92
93 drawLine(gfc, img);
94
95 //写验证码,需要定义Font
96
97 Font font = new Font("arial", 12, FontStyle.Bold);
98
99 System.Drawing.Drawing2D.LinearGradientBrush brush =
100
101 new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
102
103 gfc.DrawString(strValue, font, brush, 3, 2);
104
105 drawPoint(img);
106
107 gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
108
109 //将图像添加到页面
110
111 MemoryStream ms = new MemoryStream();
112
113 img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
114
115 //更改Http头
116
117 Response.ClearContent();
118
119 Response.ContentType = "image/gif";
120
121 Response.BinaryWrite(ms.ToArray());
122
123 //Dispose
124
125 gfc.Dispose();
126
127 img.Dispose();
128
129 Response.End();
130
131 }
132
133 private void drawLine(Graphics gfc, Bitmap img)
134
135 {
136
137 //选择画10条线,也可以增加,也可以不要线,只要随机杂点即可
138
139 for (int i = 0; i < 10; i++)
140
141 {
142
143 int x1 = ran.Next(img.Width);
144
145 int y1 = ran.Next(img.Height);
146
147 int x2 = ran.Next(img.Width);
148
149 int y2 = ran.Next(img.Height);
150
151 gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚
152
153 }
154
155 }
156
157 private void drawPoint(Bitmap img)
158
159 {
160
161 /*
162
163 //选择画100个点,可以根据实际情况改变
164
165 for (int i = 0; i < 100; i++)
166
167 {
168
169 int x = ran.Next(img.Width);
170
171 int y = ran.Next(img.Height);
172
173 img.SetPixel(x,y,Color.FromArgb(ran.Next()));//杂点颜色随机
174
175 }
176
177 */
178
179 int col = ran.Next();//在一次的图片中杂店颜色相同
180
181 for (int i = 0; i < 100; i++)
182
183 {
184
185 int x = ran.Next(img.Width);
186
187 int y = ran.Next(img.Height);
188
189 img.SetPixel(x, y, Color.FromArgb(col));
190
191 }
192
193 }
194
195 }
196
197 }


 

如何使用:

          新建一个页面WebForm1.aspx,至少需要一个文本框、image控件和一个Button控件。需要注意的是要将image控件的imageUrl指定为createImg.aspx

 

View Code
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;




namespace WebCA

{

public partial class WebForm1 : System.Web.UI.Page

{

Random ran = new Random();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

string str = getRandomValidate(4);

Session["check"] = str;

if (Session["check"] == null)

message.Text = "sorry,the image is wrong!";

}

}

private string getRandomValidate(int len)

{

int num;

int tem;

string rtuStr = "";

for (int i = 0; i < len; i++)

{

num = ran.Next();

/*

* 这里可以选择生成字符和数字组合的验证码

*/

tem = num % 10 + '0';//生成数字

//tem = num % 26 + 'A';//生成字符

rtuStr += Convert.ToChar(tem).ToString();

}

return rtuStr;

}

protected void Button1_Click(object sender, EventArgs e)

{

string a = Session["check"].ToString();

string b = check.Text;

if (check.Text.ToString() == Session["check"].ToString())

message.Text = "Yes,you pass it";

else

message.Text = "I'm sorry!try again...";

}

}

}

 

 

2010年10月13日 - songjiali_1990 - songjiali的博客
posted @ 2011-11-16 15:47  宋佳莉  阅读(426)  评论(0编辑  收藏  举报