生成验证码
下面是一个 完整的验证码 的例子,我每次使用都是去BaiDu找 以后再用可以来自己的Blog看了 哈哈!

Code
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Drawing.Imaging;
7
using System.Web;
8
using System.Web.SessionState;
9
using System.Web.UI;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.HtmlControls;
12
13
public partial class yanzzengma : System.Web.UI.Page
14

{
15
// 验证码长度
16
private int codeLen = 4;
17
// 图片清晰度
18
private int fineness =90;
19
// 图片宽度
20
private int imgWidth = 55;
21
// 图片高度
22
private int imgHeight = 24;
23
// 字体家族名称
24
private string fontFamily = "Times New Roman";
25
// 字体大小
26
private int fontSize = 14;
27
// 字体样式
28
private int fontStyle = 0;
29
// 绘制起始坐标 X
30
private int posX = 0;
31
// 绘制起始坐标 Y
32
private int posY = 0;
33
protected void Page_Load(object sender, EventArgs e)
34
{
35
读取 Request 传递参数#region 读取 Request 传递参数
36
// 获取代码长度设置
37
if (Request["CodeLen"] != null)
38
{
39
try
40
{
41
codeLen = Int32.Parse(Request["CodeLen"]);
42
43
// 规定验证码长度
44
if (codeLen < 4 || codeLen > 16)
45
throw new Exception("验证码长度必须在4到16之间");
46
}
47
catch (Exception Ex)
48
{
49
throw Ex;
50
}
51
}
52
53
// 获取图片清晰度设置
54
if (Request["Fineness"] != null)
55
{
56
try
57
{
58
fineness = Int32.Parse(Request["Fineness"]);
59
60
// 验证清晰度
61
if (fineness < 0 || fineness > 100)
62
throw new Exception("图片清晰度必须在0到100之间");
63
}
64
catch (Exception Ex)
65
{
66
throw Ex;
67
}
68
}
69
70
// 获取图片宽度
71
if (Request["ImgWidth"] != null)
72
{
73
try
74
{
75
imgWidth = Int32.Parse(Request["ImgWidth"]);
76
77
if (imgWidth < 16 || imgWidth > 480)
78
throw new Exception("图片宽度必须在16到480之间");
79
}
80
catch (Exception Ex)
81
{
82
throw Ex;
83
}
84
}
85
86
// 获取图片高度
87
if (Request["ImgHeight"] != null)
88
{
89
try
90
{
91
imgWidth = Int32.Parse(Request["ImgHeight"]);
92
93
if (imgWidth < 16 || imgWidth > 320)
94
throw new Exception("图片高度必须在16到320之间");
95
}
96
catch (Exception Ex)
97
{
98
throw Ex;
99
}
100
}
101
102
// 获取验证码字体家族名称
103
if (Request["FontFamily"] != null)
104
{
105
fontFamily = Request["FontFamily"];
106
}
107
108
// 获取验证码字体大小
109
if (Request["FontSize"] != null)
110
{
111
try
112
{
113
fontSize = Int32.Parse(Request["FontSize"]);
114
115
if (fontSize < 8 || fontSize > 72)
116
throw new Exception("字体大小必须在8到72之间");
117
}
118
catch (Exception Ex)
119
{
120
throw Ex;
121
}
122
}
123
124
// 获取字体样式
125
if (Request["FontStyle"] != null)
126
{
127
try
128
{
129
fontStyle = Int32.Parse(Request["FontStyle"]);
130
}
131
catch (Exception Ex)
132
{
133
throw Ex;
134
}
135
}
136
137
// 验证码绘制起始位置 X
138
if (Request["PosX"] != null)
139
{
140
try
141
{
142
posX = Int32.Parse(Request["PosX"]);
143
}
144
catch (Exception Ex)
145
{
146
throw Ex;
147
}
148
}
149
150
// 验证码绘制起始位置 Y
151
if (Request["PosY"] != null)
152
{
153
try
154
{
155
posY = Int32.Parse(Request["PosY"]);
156
}
157
catch (Exception Ex)
158
{
159
throw Ex;
160
}
161
}
162
#endregion
163
164
string validateCode = CreateValidateCode();
165
166
// 生成BITMAP图像
167
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
168
169
// 给图像设置干扰
170
DisturbBitmap(bitmap);
171
172
// 绘制验证码图像
173
DrawValidateCode(bitmap, validateCode);
174
175
// 保存验证码图像,等待输出
176
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
177
}
178
179
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
180
override protected void OnInit(EventArgs e)
181
{
182
//
183
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
184
//
185
InitializeComponent();
186
base.OnInit(e);
187
}
188
189
/**//// <summary>
190
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
191
/// 此方法的内容。
192
/// </summary>
193
private void InitializeComponent()
194
{
195
this.Load += new System.EventHandler(this.Page_Load);
196
}
197
#endregion
198
199
//------------------------------------------------------------
200
// 随机生成验证码,并保存到SESSION中
201
//------------------------------------------------------------
202
private string CreateValidateCode()
203
{
204
string validateCode = "";
205
int num=0;
206
// 随机数对象
207
Random random = new Random();
208
209
for (int i = 0; i < codeLen; i++)
210
{
211
// 26: a - z
212
int n = random.Next(26);
213
214
215
// 将数字转换成大写字母
216
validateCode += (char)(n + 65);
217
}
218
219
// 保存验证码
220
Session["ValidateCode"] = validateCode;
221
222
return validateCode;
223
}
224
225
//------------------------------------------------------------
226
// 为图片设置干扰点
227
//------------------------------------------------------------
228
private void DisturbBitmap(Bitmap bitmap)
229
{
230
// 通过随机数生成
231
Random random = new Random();
232
233
for (int i = 0; i < bitmap.Width; i++)
234
{
235
for (int j = 0; j < bitmap.Height; j++)
236
{
237
if (random.Next(100) <= this.fineness)
238
bitmap.SetPixel(i, j, Color.White);
239
}
240
}
241
}
242
243
//------------------------------------------------------------
244
// 绘制验证码图片
245
//------------------------------------------------------------
246
private void DrawValidateCode(Bitmap bitmap, string validateCode)
247
{
248
// 获取绘制器对象
249
Graphics g = Graphics.FromImage(bitmap);
250
251
// 设置绘制字体
252
Font font = new Font(fontFamily, fontSize, GetFontStyle());
253
254
// 绘制验证码图像
255
g.DrawString(validateCode, font, Brushes.Black, posX, posY);
256
}
257
258
//------------------------------------------------------------
259
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
260
//------------------------------------------------------------
261
private FontStyle GetFontStyle()
262
{
263
if (fontStyle == 1)
264
return FontStyle.Bold;
265
else if (fontStyle == 2)
266
return FontStyle.Italic;
267
else if (fontStyle == 3)
268
return FontStyle.Bold | FontStyle.Italic;
269
else
270
return FontStyle.Regular;
271
}
272
273
274
}
1
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Drawing.Imaging;7
using System.Web;8
using System.Web.SessionState;9
using System.Web.UI;10
using System.Web.UI.WebControls;11
using System.Web.UI.HtmlControls;12

13
public partial class yanzzengma : System.Web.UI.Page14


{15
// 验证码长度16
private int codeLen = 4;17
// 图片清晰度18
private int fineness =90;19
// 图片宽度20
private int imgWidth = 55;21
// 图片高度22
private int imgHeight = 24;23
// 字体家族名称24
private string fontFamily = "Times New Roman";25
// 字体大小26
private int fontSize = 14;27
// 字体样式28
private int fontStyle = 0;29
// 绘制起始坐标 X30
private int posX = 0;31
// 绘制起始坐标 Y32
private int posY = 0;33
protected void Page_Load(object sender, EventArgs e)34

{35

读取 Request 传递参数#region 读取 Request 传递参数36
// 获取代码长度设置37
if (Request["CodeLen"] != null)38

{39
try40

{41
codeLen = Int32.Parse(Request["CodeLen"]);42

43
// 规定验证码长度44
if (codeLen < 4 || codeLen > 16)45
throw new Exception("验证码长度必须在4到16之间");46
}47
catch (Exception Ex)48

{49
throw Ex;50
}51
}52

53
// 获取图片清晰度设置54
if (Request["Fineness"] != null)55

{56
try57

{58
fineness = Int32.Parse(Request["Fineness"]);59

60
// 验证清晰度61
if (fineness < 0 || fineness > 100)62
throw new Exception("图片清晰度必须在0到100之间");63
}64
catch (Exception Ex)65

{66
throw Ex;67
}68
}69

70
// 获取图片宽度71
if (Request["ImgWidth"] != null)72

{73
try74

{75
imgWidth = Int32.Parse(Request["ImgWidth"]);76

77
if (imgWidth < 16 || imgWidth > 480)78
throw new Exception("图片宽度必须在16到480之间");79
}80
catch (Exception Ex)81

{82
throw Ex;83
}84
}85

86
// 获取图片高度87
if (Request["ImgHeight"] != null)88

{89
try90

{91
imgWidth = Int32.Parse(Request["ImgHeight"]);92

93
if (imgWidth < 16 || imgWidth > 320)94
throw new Exception("图片高度必须在16到320之间");95
}96
catch (Exception Ex)97

{98
throw Ex;99
}100
}101

102
// 获取验证码字体家族名称103
if (Request["FontFamily"] != null)104

{105
fontFamily = Request["FontFamily"];106
}107

108
// 获取验证码字体大小109
if (Request["FontSize"] != null)110

{111
try112

{113
fontSize = Int32.Parse(Request["FontSize"]);114

115
if (fontSize < 8 || fontSize > 72)116
throw new Exception("字体大小必须在8到72之间");117
}118
catch (Exception Ex)119

{120
throw Ex;121
}122
}123

124
// 获取字体样式125
if (Request["FontStyle"] != null)126

{127
try128

{129
fontStyle = Int32.Parse(Request["FontStyle"]);130
}131
catch (Exception Ex)132

{133
throw Ex;134
}135
}136

137
// 验证码绘制起始位置 X138
if (Request["PosX"] != null)139

{140
try141

{142
posX = Int32.Parse(Request["PosX"]);143
}144
catch (Exception Ex)145

{146
throw Ex;147
}148
}149

150
// 验证码绘制起始位置 Y151
if (Request["PosY"] != null)152

{153
try154

{155
posY = Int32.Parse(Request["PosY"]);156
}157
catch (Exception Ex)158

{159
throw Ex;160
}161
}162
#endregion163

164
string validateCode = CreateValidateCode();165

166
// 生成BITMAP图像167
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);168

169
// 给图像设置干扰170
DisturbBitmap(bitmap);171

172
// 绘制验证码图像173
DrawValidateCode(bitmap, validateCode);174

175
// 保存验证码图像,等待输出176
bitmap.Save(Response.OutputStream, ImageFormat.Gif);177
}178

179

Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码180
override protected void OnInit(EventArgs e)181

{182
//183
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。184
//185
InitializeComponent();186
base.OnInit(e);187
}188

189

/**//// <summary>190
/// 设计器支持所需的方法 - 不要使用代码编辑器修改191
/// 此方法的内容。192
/// </summary>193
private void InitializeComponent()194

{195
this.Load += new System.EventHandler(this.Page_Load);196
}197
#endregion198

199
//------------------------------------------------------------200
// 随机生成验证码,并保存到SESSION中201
//------------------------------------------------------------202
private string CreateValidateCode()203

{204
string validateCode = "";205
int num=0;206
// 随机数对象207
Random random = new Random();208

209
for (int i = 0; i < codeLen; i++)210

{211
// 26: a - z212
int n = random.Next(26);213
214

215
// 将数字转换成大写字母216
validateCode += (char)(n + 65);217
}218
219
// 保存验证码220
Session["ValidateCode"] = validateCode;221

222
return validateCode;223
}224

225
//------------------------------------------------------------226
// 为图片设置干扰点227
//------------------------------------------------------------228
private void DisturbBitmap(Bitmap bitmap)229

{230
// 通过随机数生成231
Random random = new Random();232

233
for (int i = 0; i < bitmap.Width; i++)234

{235
for (int j = 0; j < bitmap.Height; j++)236

{237
if (random.Next(100) <= this.fineness)238
bitmap.SetPixel(i, j, Color.White);239
}240
}241
}242

243
//------------------------------------------------------------244
// 绘制验证码图片245
//------------------------------------------------------------246
private void DrawValidateCode(Bitmap bitmap, string validateCode)247

{248
// 获取绘制器对象249
Graphics g = Graphics.FromImage(bitmap);250

251
// 设置绘制字体252
Font font = new Font(fontFamily, fontSize, GetFontStyle());253

254
// 绘制验证码图像255
g.DrawString(validateCode, font, Brushes.Black, posX, posY);256
}257

258
//------------------------------------------------------------259
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体260
//------------------------------------------------------------261
private FontStyle GetFontStyle()262

{263
if (fontStyle == 1)264
return FontStyle.Bold;265
else if (fontStyle == 2)266
return FontStyle.Italic;267
else if (fontStyle == 3)268
return FontStyle.Bold | FontStyle.Italic;269
else270
return FontStyle.Regular;271
}272

273

274
}
浙公网安备 33010602011771号