码迷,mamicode.com
首页 > Windows程序 > 详细

C#生成验证码

时间:2015-11-19 18:25:06      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

 1   public static byte[] GetVerifyCode(string codeStr)
 2         {
 3             Random r = new Random();
 4             Bitmap btBitmap = new Bitmap(80, 30);
 5 
 6             Graphics g = Graphics.FromImage(btBitmap);
 7             g.Clear(Color.White);
 8             //画干扰线
 9             for (int i = 0; i < 20; i++)
10             {
11                 int x1 = r.Next(btBitmap.Width);
12                 int y1 = r.Next(btBitmap.Height);
13                 int x2 = r.Next(btBitmap.Width);
14                 int y2 = r.Next(btBitmap.Height);
15                 g.DrawLine(new Pen(Color.Black),x1,y1,x2,y2 );
16             }
17             //画干扰点
18             for (int i = 0; i < 100; i++)
19             {
20                 int x1 = r.Next(btBitmap.Width);
21                 int y1 = r.Next(btBitmap.Height);
22                 btBitmap.SetPixel(x1,y1,Color.Aqua);
23             }
24             //画图
25             g.DrawString(codeStr, new Font("宋体", 16), new SolidBrush(Color.Red), 0, 0);
26             MemoryStream ms = new MemoryStream();
27             btBitmap.Save(ms, ImageFormat.Gif);
28             return ms.ToArray();
29         }

 

C#生成验证码

标签:

原文地址:http://www.cnblogs.com/niceskyfly/p/4977959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!