结合网上其他的一些资料,该cs页可以随机生成 任何位数的中文验证码,调用时只需要
<img src="该页的地址"/> 即可。
<img src="该页的地址"/> 即可。
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Text;
13public partial class CheckCode : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Encoding gb = Encoding.GetEncoding("gb2312");
18
19 //调用函数产生4个随机中文汉字编码
20 object[] bytes = CreateRegionCode(4);
21
22 //根据汉字编码的字节数组解码出中文汉字
23 string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
24 string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
25 string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
26 // string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
27
28 //输出的控制台
29 // Console.WriteLine(str1 + str2 + str3 + str4);
30 // Response.Write(str1 + str2 + str3 + str4);
31 // return;
32
33 string srs = str1 + str2 + str3;// +str4;
34 Bitmap srBmp = srBitmap(srs);
35 System.IO.MemoryStream srMS = new System.IO.MemoryStream();
36 srBmp.Save(srMS, System.Drawing.Imaging.ImageFormat.Gif);
37 Response.ClearContent();
38 Response.ContentType = "image/gif";
39 Response.BinaryWrite(srMS.ToArray());
40 srBmp.Dispose();
41 }
42
43 /// <summary>
44 /// 根据指定参数返回BitMap对象
45 /// 引用如下:
46 /// using System.Drawing;
47 /// 调用例子如下:
48 /// eg1、保存为图象文件为
49 /// Bitmap srBmp = srBitmap(srs);
50 /// srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
51 /// srBmp.Dispose();
52 /// eg2。网页中调用方式如下
53 /// Bitmap srBmp = srBitmap(srs);
54 /// System.IO.MemoryStream srMS = new System.IO.MemoryStream();
55 /// srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
56 /// Response.ClearContent();
57 /// Response.ContentType = "image/gif";
58 /// Response.BinaryWrite(srMS.ToArray());
59 /// srBmp.Dispose();
60 /// </summary>
61 /// <param name="srs"></param>
62 /// <returns></returns>
63 public static Bitmap srBitmap(string srs)
64 {
65 //定义图片弯曲的角度
66 int srseedangle = 10;
67 //定义图象
68 Bitmap srBmp = new Bitmap(srs.Length * 25, 30);
69 //画图
70 Graphics srGraph = Graphics.FromImage(srBmp);
71 //清空图象
72 srGraph.Clear(Color.AliceBlue);
73 //给图象画边框
74 srGraph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, srBmp.Width - 1, srBmp.Height - 1);
75 //定义随即数
76 Random srRandom = new Random();
77 //定义画笔
78 Pen srPen = new Pen(Color.LightGray, 0);
79 //画噪点
80 for (int i = 0; i < 100; i++)
81 {
82 srGraph.DrawRectangle(srPen, srRandom.Next(1, srBmp.Width - 2), srRandom.Next(1, srBmp.Height - 2), 1, 1);
83 }
84 //将字符串转化为字符数组
85 char[] srchars = srs.ToCharArray();
86 //封状文本
87 StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
88 //设置文本垂直居中
89 srFormat.Alignment = StringAlignment.Center;
90 //设置文本水平居中
91 srFormat.LineAlignment = StringAlignment.Center;
92 //定义字体颜色
93 Color[] srColors ={ Color.Black, Color.Red, Color.DarkBlue, Color.Blue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
94 //定义字体
95 string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
96 //循环画出每个字符
97 for (int i = 0, j = srchars.Length; i < j; i++)
98 {
99 //定义字体 参数分别为字体样式 字体大小 字体字形
100 Font srFont = new Font(srFonts[srRandom.Next(5)], srRandom.Next(12, 20), FontStyle.Regular);
101 //填充图形
102 Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
103 //定义坐标
104 Point srPoint = new Point(16, 16);
105 //定义倾斜角度
106 float srangle = srRandom.Next(-srseedangle, srseedangle);
107 //倾斜
108 srGraph.TranslateTransform(srPoint.X, srPoint.Y);
109 srGraph.RotateTransform(srangle);
110 //填充字符
111 srGraph.DrawString(srchars[i].ToString(), srFont, srBrush, 1, 1, srFormat);
112 //回归正常
113 srGraph.RotateTransform(-srangle);
114 srGraph.TranslateTransform(2, -srPoint.Y);
115 }
116 srGraph.Dispose();
117 return srBmp;
118 }
119
120 /**/
121 /*
122 此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将
123 四个字节数组存储在object数组中。
124 参数:strlength,代表需要产生的汉字个数
125 */
126 public static object[] CreateRegionCode(int strlength)
127 {
128 //定义一个字符串数组储存汉字编码的组成元素
129 string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
130
131 Random rnd = new Random();
132
133 //定义一个object数组用来
134 object[] bytes = new object[strlength];
135
136 /**/
137 /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
138 每个汉字有四个区位码组成
139 区位码第1位和区位码第2位作为字节数组第一个元素
140 区位码第3位和区位码第4位作为字节数组第二个元素
141 */
142 for (int i = 0; i < strlength; i++)
143 {
144 //区位码第1位
145 int r1 = rnd.Next(11, 14);
146 string str_r1 = rBase[r1].Trim();
147
148 //区位码第2位
149 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
150 int r2;
151 if (r1 == 13)
152 {
153 r2 = rnd.Next(0, 7);
154 }
155 else
156 {
157 r2 = rnd.Next(0, 16);
158 }
159 string str_r2 = rBase[r2].Trim();
160
161 //区位码第3位
162 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
163 int r3 = rnd.Next(10, 16);
164 string str_r3 = rBase[r3].Trim();
165
166 //区位码第4位
167 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
168 int r4;
169 if (r3 == 10)
170 {
171 r4 = rnd.Next(1, 16);
172 }
173 else if (r3 == 15)
174 {
175 r4 = rnd.Next(0, 15);
176 }
177 else
178 {
179 r4 = rnd.Next(0, 16);
180 }
181 string str_r4 = rBase[r4].Trim();
182
183 //定义两个字节变量存储产生的随机汉字区位码
184 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
185 byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
186 //将两个字节变量存储在字节数组中
187 byte[] str_r = new byte[] { byte1, byte2 };
188
189 //将产生的一个汉字的字节数组放入object数组中
190 bytes.SetValue(str_r, i);
191
192 }
193
194 return bytes;
195
196 }
197
198
199}
200
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Text;
13public partial class CheckCode : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Encoding gb = Encoding.GetEncoding("gb2312");
18
19 //调用函数产生4个随机中文汉字编码
20 object[] bytes = CreateRegionCode(4);
21
22 //根据汉字编码的字节数组解码出中文汉字
23 string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
24 string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
25 string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
26 // string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
27
28 //输出的控制台
29 // Console.WriteLine(str1 + str2 + str3 + str4);
30 // Response.Write(str1 + str2 + str3 + str4);
31 // return;
32
33 string srs = str1 + str2 + str3;// +str4;
34 Bitmap srBmp = srBitmap(srs);
35 System.IO.MemoryStream srMS = new System.IO.MemoryStream();
36 srBmp.Save(srMS, System.Drawing.Imaging.ImageFormat.Gif);
37 Response.ClearContent();
38 Response.ContentType = "image/gif";
39 Response.BinaryWrite(srMS.ToArray());
40 srBmp.Dispose();
41 }
42
43 /// <summary>
44 /// 根据指定参数返回BitMap对象
45 /// 引用如下:
46 /// using System.Drawing;
47 /// 调用例子如下:
48 /// eg1、保存为图象文件为
49 /// Bitmap srBmp = srBitmap(srs);
50 /// srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
51 /// srBmp.Dispose();
52 /// eg2。网页中调用方式如下
53 /// Bitmap srBmp = srBitmap(srs);
54 /// System.IO.MemoryStream srMS = new System.IO.MemoryStream();
55 /// srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
56 /// Response.ClearContent();
57 /// Response.ContentType = "image/gif";
58 /// Response.BinaryWrite(srMS.ToArray());
59 /// srBmp.Dispose();
60 /// </summary>
61 /// <param name="srs"></param>
62 /// <returns></returns>
63 public static Bitmap srBitmap(string srs)
64 {
65 //定义图片弯曲的角度
66 int srseedangle = 10;
67 //定义图象
68 Bitmap srBmp = new Bitmap(srs.Length * 25, 30);
69 //画图
70 Graphics srGraph = Graphics.FromImage(srBmp);
71 //清空图象
72 srGraph.Clear(Color.AliceBlue);
73 //给图象画边框
74 srGraph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, srBmp.Width - 1, srBmp.Height - 1);
75 //定义随即数
76 Random srRandom = new Random();
77 //定义画笔
78 Pen srPen = new Pen(Color.LightGray, 0);
79 //画噪点
80 for (int i = 0; i < 100; i++)
81 {
82 srGraph.DrawRectangle(srPen, srRandom.Next(1, srBmp.Width - 2), srRandom.Next(1, srBmp.Height - 2), 1, 1);
83 }
84 //将字符串转化为字符数组
85 char[] srchars = srs.ToCharArray();
86 //封状文本
87 StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
88 //设置文本垂直居中
89 srFormat.Alignment = StringAlignment.Center;
90 //设置文本水平居中
91 srFormat.LineAlignment = StringAlignment.Center;
92 //定义字体颜色
93 Color[] srColors ={ Color.Black, Color.Red, Color.DarkBlue, Color.Blue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
94 //定义字体
95 string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
96 //循环画出每个字符
97 for (int i = 0, j = srchars.Length; i < j; i++)
98 {
99 //定义字体 参数分别为字体样式 字体大小 字体字形
100 Font srFont = new Font(srFonts[srRandom.Next(5)], srRandom.Next(12, 20), FontStyle.Regular);
101 //填充图形
102 Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
103 //定义坐标
104 Point srPoint = new Point(16, 16);
105 //定义倾斜角度
106 float srangle = srRandom.Next(-srseedangle, srseedangle);
107 //倾斜
108 srGraph.TranslateTransform(srPoint.X, srPoint.Y);
109 srGraph.RotateTransform(srangle);
110 //填充字符
111 srGraph.DrawString(srchars[i].ToString(), srFont, srBrush, 1, 1, srFormat);
112 //回归正常
113 srGraph.RotateTransform(-srangle);
114 srGraph.TranslateTransform(2, -srPoint.Y);
115 }
116 srGraph.Dispose();
117 return srBmp;
118 }
119
120 /**/
121 /*
122 此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将
123 四个字节数组存储在object数组中。
124 参数:strlength,代表需要产生的汉字个数
125 */
126 public static object[] CreateRegionCode(int strlength)
127 {
128 //定义一个字符串数组储存汉字编码的组成元素
129 string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
130
131 Random rnd = new Random();
132
133 //定义一个object数组用来
134 object[] bytes = new object[strlength];
135
136 /**/
137 /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
138 每个汉字有四个区位码组成
139 区位码第1位和区位码第2位作为字节数组第一个元素
140 区位码第3位和区位码第4位作为字节数组第二个元素
141 */
142 for (int i = 0; i < strlength; i++)
143 {
144 //区位码第1位
145 int r1 = rnd.Next(11, 14);
146 string str_r1 = rBase[r1].Trim();
147
148 //区位码第2位
149 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
150 int r2;
151 if (r1 == 13)
152 {
153 r2 = rnd.Next(0, 7);
154 }
155 else
156 {
157 r2 = rnd.Next(0, 16);
158 }
159 string str_r2 = rBase[r2].Trim();
160
161 //区位码第3位
162 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
163 int r3 = rnd.Next(10, 16);
164 string str_r3 = rBase[r3].Trim();
165
166 //区位码第4位
167 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
168 int r4;
169 if (r3 == 10)
170 {
171 r4 = rnd.Next(1, 16);
172 }
173 else if (r3 == 15)
174 {
175 r4 = rnd.Next(0, 15);
176 }
177 else
178 {
179 r4 = rnd.Next(0, 16);
180 }
181 string str_r4 = rBase[r4].Trim();
182
183 //定义两个字节变量存储产生的随机汉字区位码
184 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
185 byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
186 //将两个字节变量存储在字节数组中
187 byte[] str_r = new byte[] { byte1, byte2 };
188
189 //将产生的一个汉字的字节数组放入object数组中
190 bytes.SetValue(str_r, i);
191
192 }
193
194 return bytes;
195
196 }
197
198
199}
200
点个广告: