绘制验证码图片

代码
 1  public Bitmap CreateImage(string code)
 2         {
 3 
 4             Bitmap image = new Bitmap(10030);
 5             Graphics g = Graphics.FromImage(image);
 6             //WebColorConverter ww = new WebColorConverter();
 7             //g.Clear((Color)ww.ConvertFromString("#FAE264"));
 8 
 9             Random random = new Random();
10             //画图片的背景噪音线
11             for (int i = 0; i < 12; i++)
12             {
13                 int x1 = random.Next(image.Width);
14                 int x2 = random.Next(image.Width);
15                 int y1 = random.Next(image.Height);
16                 int y2 = random.Next(image.Height);
17 
18                 g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
19             }
20             Font font = new Font("Arial"15, FontStyle.Bold | FontStyle.Italic);
21             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
22                 new Rectangle(00, image.Width, image.Height), Color.Blue, Color.Gray, 1.2ftrue);
23             g.DrawString(code, font, brush, 00);
24 
25             画图片的前景噪音点
26             for (int i = 0; i < 10; i++)
27             {
28                 int x = random.Next(image.Width);
29                 int y = random.Next(image.Height);
30                 image.SetPixel(x, y, Color.White);
31             }
32 
33             //画图片的边框线
34             g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
35             g.DrawRectangle(new Pen(Color.Silver), 00, image.Width, image.Height - 1);
36 
37             System.IO.MemoryStream ms = new System.IO.MemoryStream();
38             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
39             Response.ClearContent();
40             Response.ContentType = "image/Gif";
41             Response.BinaryWrite(ms.ToArray());
42             g.Dispose();
43             image.Dispose();
44 
45             return image;
46         }

代码出处来自于网络,具体出处忘记了,不好意思。

原文地址:https://www.cnblogs.com/angleSJW/p/1626081.html