验证码ASHX版本,大写字母和数字版本
闲话少说,代码如下:
1
<%@ WebHandler Language="C#" Class="ValidateCode" %>
2
3
using System;
4
using System.Web;
5
using System.Web.SessionState;
6
using System.IO;
7
using System.Drawing;
8
using System.Drawing.Imaging;
9
10
11
public class ValidateCode : IHttpHandler, IRequiresSessionState
12

{
13
14
public void ProcessRequest (HttpContext context)
{
15
//context.Response.ContentType = "text/plain";
16
//context.Response.Write("Hello World");
17
18
// 将生成的图片发回客户端
19
string str_ValidateCode = GetRandomNumberString(4);
20
context.Session["ValidateCode"] = str_ValidateCode;
21
22
Bitmap theBitmap = CreateImage(str_ValidateCode);
23
MemoryStream ms = new MemoryStream();
24
theBitmap.Save(ms, ImageFormat.Png);
25
26
context.Response.ClearContent();
27
28
//需要输出图象信息 要修改HTTP头
29
context.Response.ContentType = "image/Png";
30
context.Response.BinaryWrite(ms.ToArray());
31
32
theBitmap.Dispose();
33
context.Response.End();
34
35
36
}
37
38
public bool IsReusable
{
39
get
{
40
return false;
41
}
42
}
43
44
45
// 生成随机数字字符串
46
public string GetRandomNumberString(int int_NumberLength)
47
{
48
string str_Number = string.Empty;
49
string[] ValidateCodes = new string[36];
50
//
51
// Put number and alphebit in ValidateCodes
52
//
53
for (int i = 0; i < 10; i++)
54
{
55
ValidateCodes[i] = i.ToString();
56
}
57
int count = 10;
58
for (int i = 65; i < 91; i++)
59
{
60
ValidateCodes[count] = ((char)i).ToString();
61
count++;
62
63
}
64
65
Random theRandomNumber = new Random();
66
67
for (int int_index = 0; int_index < int_NumberLength; int_index++)
68
str_Number += ValidateCodes[theRandomNumber.Next(36)];
69
70
return str_Number;
71
}
72
73
//生成随机颜色
74
public Color GetRandomColor()
75
{
76
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
77
// 对于C#的随机数,没什么好说的
78
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
79
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
80
81
// 为了在白色背景上显示,尽量生成深色
82
int int_Red = RandomNum_First.Next(256);
83
int int_Green = RandomNum_Sencond.Next(256);
84
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
85
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
86
87
return Color.FromArgb(int_Red, int_Green, int_Blue);
88
}
89
//根据验证字符串生成最终图象
90
public Bitmap CreateImage(string str_ValidateCode)
91
{
92
int int_ImageWidth = str_ValidateCode.Length * 15;
93
Random newRandom = new Random();
94
// 图高20px
95
Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
96
Graphics theGraphics = Graphics.FromImage(theBitmap);
97
// 白色背景
98
theGraphics.Clear(Color.White);
99
// 灰色边框
100
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
101
102
// 10pt的字体
103
Font theFont = new Font("Arial", 12);
104
105
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
106
{
107
string str_char = str_ValidateCode.Substring(int_index, 1);
108
Brush newBrush = new SolidBrush(GetRandomColor());
109
Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
110
theGraphics.DrawString(str_char, theFont, newBrush, thePos);
111
}
112
113
theGraphics.Dispose();
114
// theBitmap.Dispose();
115
return theBitmap;
116
117
118
}
119
120
}
<%@ WebHandler Language="C#" Class="ValidateCode" %>2

3
using System;4
using System.Web;5
using System.Web.SessionState;6
using System.IO;7
using System.Drawing;8
using System.Drawing.Imaging;9

10

11
public class ValidateCode : IHttpHandler, IRequiresSessionState12


{13
14

public void ProcessRequest (HttpContext context)
{15
//context.Response.ContentType = "text/plain";16
//context.Response.Write("Hello World");17
18
// 将生成的图片发回客户端19
string str_ValidateCode = GetRandomNumberString(4);20
context.Session["ValidateCode"] = str_ValidateCode;21
22
Bitmap theBitmap = CreateImage(str_ValidateCode);23
MemoryStream ms = new MemoryStream();24
theBitmap.Save(ms, ImageFormat.Png);25

26
context.Response.ClearContent(); 27
28
//需要输出图象信息 要修改HTTP头 29
context.Response.ContentType = "image/Png";30
context.Response.BinaryWrite(ms.ToArray());31
32
theBitmap.Dispose();33
context.Response.End();34
35
36
}37
38

public bool IsReusable
{39

get
{40
return false;41
}42
}43
44
45
// 生成随机数字字符串46
public string GetRandomNumberString(int int_NumberLength)47

{48
string str_Number = string.Empty;49
string[] ValidateCodes = new string[36];50
//51
// Put number and alphebit in ValidateCodes52
//53
for (int i = 0; i < 10; i++)54

{55
ValidateCodes[i] = i.ToString();56
}57
int count = 10;58
for (int i = 65; i < 91; i++)59

{60
ValidateCodes[count] = ((char)i).ToString();61
count++;62
63
}64
65
Random theRandomNumber = new Random();66

67
for (int int_index = 0; int_index < int_NumberLength; int_index++)68
str_Number += ValidateCodes[theRandomNumber.Next(36)];69

70
return str_Number;71
}72
73
//生成随机颜色74
public Color GetRandomColor()75

{76
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);77
// 对于C#的随机数,没什么好说的78
System.Threading.Thread.Sleep(RandomNum_First.Next(50));79
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);80

81
// 为了在白色背景上显示,尽量生成深色82
int int_Red = RandomNum_First.Next(256);83
int int_Green = RandomNum_Sencond.Next(256);84
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;85
int_Blue = (int_Blue > 255) ? 255 : int_Blue;86

87
return Color.FromArgb(int_Red, int_Green, int_Blue);88
}89
//根据验证字符串生成最终图象90
public Bitmap CreateImage(string str_ValidateCode)91

{92
int int_ImageWidth = str_ValidateCode.Length * 15;93
Random newRandom = new Random();94
// 图高20px95
Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);96
Graphics theGraphics = Graphics.FromImage(theBitmap);97
// 白色背景98
theGraphics.Clear(Color.White);99
// 灰色边框100
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);101

102
// 10pt的字体103
Font theFont = new Font("Arial", 12);104

105
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)106

{107
string str_char = str_ValidateCode.Substring(int_index, 1);108
Brush newBrush = new SolidBrush(GetRandomColor());109
Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));110
theGraphics.DrawString(str_char, theFont, newBrush, thePos);111
}112

113
theGraphics.Dispose();114
// theBitmap.Dispose();115
return theBitmap;116

117
118
}119
120
}
本文介绍了一种使用C#编写的验证码生成器实现方案。该方案通过生成包含大写字母和数字的随机字符串,并将其绘制为PNG格式的图片返回给客户端。文章详细展示了如何创建随机字符串、设置图片样式及发送图片到浏览器的过程。
1万+

被折叠的 条评论
为什么被折叠?



