![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
///
<summary>
2 /// 生成随机码
3 /// </summary>
4 /// <param name="length"> 随机码个数 </param>
5 /// <returns></returns>
6 private string CreateRandomCode( int length)
7 {
8 int rand;
9 char code;
10 string randomcode = String.Empty;
11
12 // 生成一定长度的验证码
13 System.Random random = new Random();
14 for ( int i = 0 ; i < length; i ++ )
15 {
16 rand = random.Next();
17
18 if (rand % 3 == 0 )
19 {
20 code = ( char )( ' A ' + ( char )(rand % 26 ));
21 }
22 else
23 {
24 code = ( char )( ' 0 ' + ( char )(rand % 10 ));
25 }
26
27 randomcode += code.ToString();
28 }
29 return randomcode;
30 }
31
32 /// <summary>
33 /// 创建随机码图片
34 /// </summary>
35 /// <param name="randomcode"> 随机码 </param>
36 private void CreateImage( string randomcode)
37 {
38 int randAngle = 45 ; // 随机转动角度
39 int mapwidth = ( int )(randomcode.Length * 16 );
40 Bitmap map = new Bitmap(mapwidth, 22 ); // 创建图片背景
41 Graphics graph = Graphics.FromImage(map);
42 graph.Clear(Color.AliceBlue); // 清除画面,填充背景
43 graph.DrawRectangle( new Pen(Color.Black, 0 ), 0 , 0 , map.Width - 1 , map.Height - 1 ); // 画一个边框
44 // graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 模式
45
46 Random rand = new Random();
47
48 // 背景噪点生成
49 Pen blackPen = new Pen(Color.LightGray, 0 );
50 for ( int i = 0 ; i < 50 ; i ++ )
51 {
52 int x = rand.Next( 0 , map.Width);
53 int y = rand.Next( 0 , map.Height);
54 graph.DrawRectangle(blackPen, x, y, 1 , 1 );
55 }
56
57
58 // 验证码旋转,防止机器识别
59 char [] chars = randomcode.ToCharArray(); // 拆散字符串成单字符数组
60
61 // 文字距中
62 StringFormat format = new StringFormat(StringFormatFlags.NoClip);
63 format.Alignment = StringAlignment.Center;
64 format.LineAlignment = StringAlignment.Center;
65
66 // 定义颜色
67 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
68 // 定义字体
69 string [] font = { " Verdana " , " Microsoft Sans Serif " , " Comic Sans MS " , " Arial " , " 宋体 " };
70 int cindex = rand.Next( 7 );
71
72 for ( int i = 0 ; i < chars.Length; i ++ )
73 {
74 int findex = rand.Next( 5 );
75
76 Font f = new System.Drawing.Font(font[findex], 14 , System.Drawing.FontStyle.Bold); // 字体样式(参数2为字体大小)
77 Brush b = new System.Drawing.SolidBrush(c[cindex]);
78
79 Point dot = new Point( 14 , 14 );
80 // graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150); // 测试X坐标显示间距的
81 float angle = rand.Next( - randAngle, randAngle); // 转动的度数
82
83 graph.TranslateTransform(dot.X, dot.Y); // 移动光标到指定位置
84 graph.RotateTransform(angle);
85 graph.DrawString(chars[i].ToString(), f, b, 1 , 1 , format);
86 // graph.DrawString(chars[i].ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);
87 graph.RotateTransform( - angle); // 转回去
88 graph.TranslateTransform( - 2 , - dot.Y); // 移动光标到指定位置,每个字符紧凑显示,避免被软件识别
89 }
92 // 生成图片
93 System.IO.MemoryStream ms = new System.IO.MemoryStream();
94 map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
95 Response.ClearContent();
96 Response.ContentType = " image/gif " ;
97 Response.BinaryWrite(ms.ToArray());
98 graph.Dispose();
99 map.Dispose();
100 }
2 /// 生成随机码
3 /// </summary>
4 /// <param name="length"> 随机码个数 </param>
5 /// <returns></returns>
6 private string CreateRandomCode( int length)
7 {
8 int rand;
9 char code;
10 string randomcode = String.Empty;
11
12 // 生成一定长度的验证码
13 System.Random random = new Random();
14 for ( int i = 0 ; i < length; i ++ )
15 {
16 rand = random.Next();
17
18 if (rand % 3 == 0 )
19 {
20 code = ( char )( ' A ' + ( char )(rand % 26 ));
21 }
22 else
23 {
24 code = ( char )( ' 0 ' + ( char )(rand % 10 ));
25 }
26
27 randomcode += code.ToString();
28 }
29 return randomcode;
30 }
31
32 /// <summary>
33 /// 创建随机码图片
34 /// </summary>
35 /// <param name="randomcode"> 随机码 </param>
36 private void CreateImage( string randomcode)
37 {
38 int randAngle = 45 ; // 随机转动角度
39 int mapwidth = ( int )(randomcode.Length * 16 );
40 Bitmap map = new Bitmap(mapwidth, 22 ); // 创建图片背景
41 Graphics graph = Graphics.FromImage(map);
42 graph.Clear(Color.AliceBlue); // 清除画面,填充背景
43 graph.DrawRectangle( new Pen(Color.Black, 0 ), 0 , 0 , map.Width - 1 , map.Height - 1 ); // 画一个边框
44 // graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 模式
45
46 Random rand = new Random();
47
48 // 背景噪点生成
49 Pen blackPen = new Pen(Color.LightGray, 0 );
50 for ( int i = 0 ; i < 50 ; i ++ )
51 {
52 int x = rand.Next( 0 , map.Width);
53 int y = rand.Next( 0 , map.Height);
54 graph.DrawRectangle(blackPen, x, y, 1 , 1 );
55 }
56
57
58 // 验证码旋转,防止机器识别
59 char [] chars = randomcode.ToCharArray(); // 拆散字符串成单字符数组
60
61 // 文字距中
62 StringFormat format = new StringFormat(StringFormatFlags.NoClip);
63 format.Alignment = StringAlignment.Center;
64 format.LineAlignment = StringAlignment.Center;
65
66 // 定义颜色
67 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
68 // 定义字体
69 string [] font = { " Verdana " , " Microsoft Sans Serif " , " Comic Sans MS " , " Arial " , " 宋体 " };
70 int cindex = rand.Next( 7 );
71
72 for ( int i = 0 ; i < chars.Length; i ++ )
73 {
74 int findex = rand.Next( 5 );
75
76 Font f = new System.Drawing.Font(font[findex], 14 , System.Drawing.FontStyle.Bold); // 字体样式(参数2为字体大小)
77 Brush b = new System.Drawing.SolidBrush(c[cindex]);
78
79 Point dot = new Point( 14 , 14 );
80 // graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150); // 测试X坐标显示间距的
81 float angle = rand.Next( - randAngle, randAngle); // 转动的度数
82
83 graph.TranslateTransform(dot.X, dot.Y); // 移动光标到指定位置
84 graph.RotateTransform(angle);
85 graph.DrawString(chars[i].ToString(), f, b, 1 , 1 , format);
86 // graph.DrawString(chars[i].ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);
87 graph.RotateTransform( - angle); // 转回去
88 graph.TranslateTransform( - 2 , - dot.Y); // 移动光标到指定位置,每个字符紧凑显示,避免被软件识别
89 }
92 // 生成图片
93 System.IO.MemoryStream ms = new System.IO.MemoryStream();
94 map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
95 Response.ClearContent();
96 Response.ContentType = " image/gif " ;
97 Response.BinaryWrite(ms.ToArray());
98 graph.Dispose();
99 map.Dispose();
100 }
用法为新建一个空页面,清除掉页面上的HTML;然后复制一下代码到页面中
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
protected
void
Page_Load(
object
sender, EventArgs e)
2 {
4 string randomcode = CreateRandomCode(4);
2 {
4 string randomcode = CreateRandomCode(4);
5
Session[
"
Code
"
]
=
randomcode;
6 // v.CreateValidateGraphic(Page, Session["Code"].ToString());
7 this .CreateImage(randomcode);
8 }
6 // v.CreateValidateGraphic(Page, Session["Code"].ToString());
7 this .CreateImage(randomcode);
8 }
最近公司做一个投票系统,为了避免刷票机自动刷票;在投票按钮上要求输入验证码;
受到QQ网站验证码的启发;经过多次试验,本方案能避免被软件识别;
现在发出来分享。