CheckCode.aspx.cs:
1public partial class _Default : System.Web.UI.Page
2{
3protected void Page_Load(object sender, EventArgs e)
4{
5GraphicsImage(4); //调用方法生成四位汉字验证码
6}
7
8private object[] CreateString(int strlength)
9{
10//定义一个数组存储汉字编码的组成元素
11string[] str = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
12"e", "f" };
13
14Random ran = new Random(); //定义一个随机数对象
15object[] bytes = new object[strlength];
16for (int i = 0; i < strlength; i++)
17{
18//获取区位码第一位
19int ran1 = ran.Next(11, 14);
20string str1 = str[ran1].Trim();
21
22//获取区位码第二位并防止数据重复
23ran = new Random(ran1 * unchecked((int)DateTime.Now.Ticks) + i);
24int ran2;
25if (ran1 == 13)
26{
27ran2 = ran.Next(0, 7);
28}
29else
30{
31ran2 = ran.Next(0, 16);
32}
33string str2 = str[ran2].Trim();
34
35//获取区位码第三位
36ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i);
37int ran3 = ran.Next(10, 16);
38string str3 = str[ran3].Trim();
39
40//获取区位码第四位
41ran = new Random(ran3 * unchecked((int)DateTime.Now.Ticks) + i);
42int ran4;
43if (ran3 == 10)
44{
45ran4 = ran.Next(1, 16);
46}
47else if (ran3 == 15)
48{
49ran4 = ran.Next(0, 15);
50}
51else
52{
53ran4 = ran.Next(0, 16);
54}
55string str4 = str[ran4].Trim();
56
57//定义字节变量存储产生的随机汉字区位码
58byte byte1 = Convert.ToByte(str1 + str2, 16);
59byte byte2 = Convert.ToByte(str3 + str4, 16);
60
61byte[] stradd = new byte[] { byte1,byte2};
62//将产生的汉字字节放入数组
63bytes.SetValue(stradd, i);
64}
65return bytes;
66}
67
68private string GetString(int length)
69{
70Encoding gb = Encoding.GetEncoding("gb2312");
71object[] bytes = CreateString(length);
72
73//根据汉字字节解码出中文汉字
74string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
75string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
76string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
77string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
78
79string str = str1 + str2 + str3 + str4;
80Response.Cookies.Add(new HttpCookie("CheckCode", str));
81return str;
82}
83
84private void GraphicsImage(int length)
85{
86System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length *
8722.5)), 22);
88Graphics g = Graphics.FromImage(image); //创建画布
89
90try
91{
92//生成随机生成器
93Random random = new Random();
94
95//清空图片背景色
96g.Clear(Color.White);
97
98//画图片的背景噪音线
99for (int i = 0; i < 1; i++)
100{
101int x1 = random.Next(image.Width);
102int x2 = random.Next(image.Width);
103int y1 = random.Next(image.Height);
104int y2 = random.Next(image.Height);
105
106g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
107}
108
109Font font = new System.Drawing.Font("Couriew New", 12, System.Drawing.FontStyle.Bold );
110System.Drawing.Drawing2D.LinearGradientBrush brush = new
111System.Drawing.Drawing2D.LinearGradientBrush
112(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
113g.DrawString(GetString(length), font, brush, 2, 2);
114
115//画图片的前景噪音点
116for (int i = 0; i < 50; i++)
117{
118int x = random.Next(image.Width);
119int y = random.Next(image.Height);
120
121image.SetPixel(x, y, Color.FromArgb(random.Next()));
122}
123
124//画图片的边框线
125g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
126System.IO.MemoryStream ms = new System.IO.MemoryStream();
127image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
128Response.ClearContent();
129Response.ContentType = "image/Gif";
130Response.BinaryWrite(ms.ToArray());
131}
132catch (Exception ms)
133{
134Response.Write(ms.Message);
135}
136}
137}
138
引用文件:
2{
3protected void Page_Load(object sender, EventArgs e)
4{
5GraphicsImage(4); //调用方法生成四位汉字验证码
6}
7
8private object[] CreateString(int strlength)
9{
10//定义一个数组存储汉字编码的组成元素
11string[] str = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
12"e", "f" };
13
14Random ran = new Random(); //定义一个随机数对象
15object[] bytes = new object[strlength];
16for (int i = 0; i < strlength; i++)
17{
18//获取区位码第一位
19int ran1 = ran.Next(11, 14);
20string str1 = str[ran1].Trim();
21
22//获取区位码第二位并防止数据重复
23ran = new Random(ran1 * unchecked((int)DateTime.Now.Ticks) + i);
24int ran2;
25if (ran1 == 13)
26{
27ran2 = ran.Next(0, 7);
28}
29else
30{
31ran2 = ran.Next(0, 16);
32}
33string str2 = str[ran2].Trim();
34
35//获取区位码第三位
36ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i);
37int ran3 = ran.Next(10, 16);
38string str3 = str[ran3].Trim();
39
40//获取区位码第四位
41ran = new Random(ran3 * unchecked((int)DateTime.Now.Ticks) + i);
42int ran4;
43if (ran3 == 10)
44{
45ran4 = ran.Next(1, 16);
46}
47else if (ran3 == 15)
48{
49ran4 = ran.Next(0, 15);
50}
51else
52{
53ran4 = ran.Next(0, 16);
54}
55string str4 = str[ran4].Trim();
56
57//定义字节变量存储产生的随机汉字区位码
58byte byte1 = Convert.ToByte(str1 + str2, 16);
59byte byte2 = Convert.ToByte(str3 + str4, 16);
60
61byte[] stradd = new byte[] { byte1,byte2};
62//将产生的汉字字节放入数组
63bytes.SetValue(stradd, i);
64}
65return bytes;
66}
67
68private string GetString(int length)
69{
70Encoding gb = Encoding.GetEncoding("gb2312");
71object[] bytes = CreateString(length);
72
73//根据汉字字节解码出中文汉字
74string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
75string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
76string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
77string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
78
79string str = str1 + str2 + str3 + str4;
80Response.Cookies.Add(new HttpCookie("CheckCode", str));
81return str;
82}
83
84private void GraphicsImage(int length)
85{
86System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length *
8722.5)), 22);
88Graphics g = Graphics.FromImage(image); //创建画布
89
90try
91{
92//生成随机生成器
93Random random = new Random();
94
95//清空图片背景色
96g.Clear(Color.White);
97
98//画图片的背景噪音线
99for (int i = 0; i < 1; i++)
100{
101int x1 = random.Next(image.Width);
102int x2 = random.Next(image.Width);
103int y1 = random.Next(image.Height);
104int y2 = random.Next(image.Height);
105
106g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
107}
108
109Font font = new System.Drawing.Font("Couriew New", 12, System.Drawing.FontStyle.Bold );
110System.Drawing.Drawing2D.LinearGradientBrush brush = new
111System.Drawing.Drawing2D.LinearGradientBrush
112(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
113g.DrawString(GetString(length), font, brush, 2, 2);
114
115//画图片的前景噪音点
116for (int i = 0; i < 50; i++)
117{
118int x = random.Next(image.Width);
119int y = random.Next(image.Height);
120
121image.SetPixel(x, y, Color.FromArgb(random.Next()));
122}
123
124//画图片的边框线
125g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
126System.IO.MemoryStream ms = new System.IO.MemoryStream();
127image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
128Response.ClearContent();
129Response.ContentType = "image/Gif";
130Response.BinaryWrite(ms.ToArray());
131}
132catch (Exception ms)
133{
134Response.Write(ms.Message);
135}
136}
137}
138
1
protected
void
Button1_Click(
object
sender, EventArgs e)
2 {
3 HttpCookie cookie = Request.Cookies["CheckCode"];
4 if (cookie.Value == this.TextBox3.Text.Trim())
5 {
6 Response.Write("<script>alert('验证码正确!')</script>");
7 }
8 else
9 {
10 Response.Write("<script>alert('验证码错误!')</script>");
11 }
12 }
2 {
3 HttpCookie cookie = Request.Cookies["CheckCode"];
4 if (cookie.Value == this.TextBox3.Text.Trim())
5 {
6 Response.Write("<script>alert('验证码正确!')</script>");
7 }
8 else
9 {
10 Response.Write("<script>alert('验证码错误!')</script>");
11 }
12 }