C#生成验证码

 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         }

 

posted @ 2015-11-19 16:08  NSkG  阅读(137)  评论(0编辑  收藏  举报