首页 > Windows开发 > 详细

C#生成验证码

时间:2015-11-19 18:21:58      阅读:285      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!