ASP.NET中常用功能代码总结(2)——生成验证图片<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
整理:Terrylee
生成随机的验证图片
使用方法:
1
/**/
///调用函数将验证码生成图片
2
this
.CreateCheckCodeImage(GenerateCheckCode());
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
1
/**/
/// <summary>
2
/// 产生五位的随机字符串
3
/// </summary>
4
/// <returns></returns>
5
private
string
GenerateCheckCode()
6![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7
int number;
8
char code;
9
string checkCode = String.Empty;
10![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
System.Random random = new Random();
12![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
for (int i = 0; i < 5; i++)
14![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
number = random.Next();
16![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
17
if (number % 2 == 0)
18
code = (char)('0' + (char)(number % 10));
19
else
20
code = (char)('A' + (char)(number % 26));
21![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
checkCode += code.ToString();
23
}
24![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////用于客户端校验码比较
26
Session["CheckCode"] = checkCode;
27![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
return checkCode;
29
}
30
31![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
32
/// 将验证码生成图片显示
33
/// </summary>
34
/// <param name="checkCode"></param>
35
private
void
CreateCheckCodeImage(
string
checkCode)
36![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
37
if (checkCode == null || checkCode.Trim() == String.Empty)
38
return;
39![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
40
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
41
Graphics g = Graphics.FromImage(image);
42![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
try
44![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////生成随机生成器
46
Random random = new Random();
47![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
48![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////清空图片背景色
49
g.Clear(Color.White);
50![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////画图片的背景噪音线
52
for (int i = 0; i < 25; i++)
53![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
int x1 = random.Next(image.Width);
55
int x2 = random.Next(image.Width);
56
int y1 = random.Next(image.Height);
57
int y2 = random.Next(image.Height);
58![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
59
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
60
}
61![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
62
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
63
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);
64
g.DrawString(checkCode, font, brush, 2, 2);
65![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////画图片的前景噪音点
67
for (int i = 0; i < 100; i++)
68![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
int x = random.Next(image.Width);
70
int y = random.Next(image.Height);
71![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
72
image.SetPixel(x, y, Color.FromArgb(random.Next()));
73
}
74![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////画图片的边框线
76
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
77![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
78
System.IO.MemoryStream ms = new System.IO.MemoryStream();
79
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
80
Response.ClearContent();
81
Response.ContentType = "image/Gif";
82
Response.BinaryWrite(ms.ToArray());
83
}
84
finally
85![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
g.Dispose();
87
image.Dispose();
88
}
89
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![None.gif](/Images/OutliningIndicators/None.gif)
31
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
35
![None.gif](/Images/OutliningIndicators/None.gif)
36
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
45
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
48
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
49
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
57
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
58
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
59
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
62
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
63
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
64
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
65
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
67
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
68
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
69
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
72
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
73
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
76
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
77
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
78
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
79
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
80
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
81
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
82
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
83
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
85
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
86
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
87
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
88
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)