1 验证码样式一
 2          protected void Page_Load(object sender, EventArgs e)
 3          {
 4              if (!Page.IsPostBack)
 5              {
 6                  this.GenImg(this.GenCode(4));
 7              }
 8          }
 9          private string GenCode(int num)
10          {
11              string[] source ={"1","2","3","4","5","6","7","8","9",
12                           "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
13                         "P","Q","R","S","T","U","V","W","X","Y","Z"};
14              string code = "";
15              Random rd = new Random();
16              for (int i = 0; i < num; i++)
17              {
18                  code += source[rd.Next(0, source.Length)];
19              }
20              return code;
21          }
22  
23          //生成图片
24          private void GenImg(string code)
25          {
26              Bitmap myPalette = new Bitmap(40, 18);
27  
28              Graphics gh = Graphics.FromImage(myPalette);
29  
30              Rectangle rc = new Rectangle(0, 0, 40, 18);
31  
32              gh.FillRectangle(new SolidBrush(Color.Gray), rc);
33              gh.DrawString(code, new Font("宋体", 12), new SolidBrush(Color.White), rc);
34  
35              myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
36  
37              Session["ValidateCode"] = code;
38  
39              gh.Dispose();
40              myPalette.Dispose();
41          }

 1 验证码样式二
 2   protected void Page_Load(object sender, EventArgs e)
 3          {
 4              CreateCheckCodeImage(GenerateCheckCode());
 5          }
 6  
 7          //随机生成验证码
 8          private string GenerateCheckCode()
 9          {
10              int number;
11              char code;
12              string checkCode = String.Empty;
13              System.Random random = new Random();
14              for (int i = 0; i < 5; i++)
15              {
16                  number = random.Next();
17                  if (number % 2 == 0)

18                      code = (char)('0' + (char)(number % 10));
19                  else
20                      code = (char)('A' + (char)(number % 26));
21                  checkCode += code.ToString();
22              }
23              Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
24              return checkCode;
25          }
26  
27          //生成验证码图片
28          private void CreateCheckCodeImage(string checkCode)
29          {
30              if (checkCode == null || checkCode.Trim() == String.Empty)
31                  return;
32              System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
33              Graphics g = Graphics.FromImage(image);
34              try
35              {
36                  Random random = new Random();                       
37                  g.Clear(Color.White);                
38                  for (int i = 0; i < 25; i++)
39                  {
40                      int x1 = random.Next(image.Width);
41                      int x2 = random.Next(image.Width);
42                      int y1 = random.Next(image.Height);
43                      int y2 = random.Next(image.Height);
44                      g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
45                  }
46  
47                  Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
48                  System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
49                  g.DrawString(checkCode, font, brush, 2, 2);               
50                  for (int i = 0; i < 100; i++)
51                  {
52                      int x = random.Next(image.Width);
53                      int y = random.Next(image.Height);
54  
55                      image.SetPixel(x, y, Color.FromArgb(random.Next()));
56                  }                
57                  g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
58                  System.IO.MemoryStream ms = new System.IO.MemoryStream();
59                  image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
60                  Response.ClearContent();
61                  Response.ContentType = "image/Gif";
62                  Response.BinaryWrite(ms.ToArray());
63              }
64              finally
65              {
66                  g.Dispose();
67                  image.Dispose();
68              }
69          }

 1 验证码样式三
 2   private static Random rnd = new Random();
 3  
 4          protected void Page_Load(object sender, EventArgs e)
 5          {            
 6              int width = 60;
 7              int height = 20;            
 8              Bitmap img = new Bitmap(width, height);           
 9              Graphics g = Graphics.FromImage(img);           
10              Brush b;           
11              b = new SolidBrush(Color.FromArgb(rnd.Next(200, 250), rnd.Next(200, 250), rnd.Next(200, 250)));            
12              g.FillRectangle(b, 0, 0, width, height);
13              Pen p = new Pen(Color.FromArgb(rnd.Next(160, 200), rnd.Next(160, 200), rnd.Next(160, 200)));
14              for (int i = 0; i < 100; i++)
15              {
16                  int x1 = rnd.Next(width);
17                  int y1 = rnd.Next(height);              
18                  int x2 = x1 + rnd.Next(12);
19                  int y2 = y1 + rnd.Next(12);               
20                  g.DrawLine(p, x1, y1, x2, y2);
21              }            
22              Font f = new Font("Courier New", 16, FontStyle.Bold);            
23              PointF pf;          
24              StringBuilder sb = new StringBuilder();            
25              for (int i = 0; i < 4; i++)
26              {                
27                  string s = rnd.Next(10).ToString();
28                  sb.Append(s);
29                  b = new SolidBrush(Color.FromArgb(rnd.Next(20, 130), rnd.Next(20, 130), rnd.Next(20, 130)));                
30                  pf = new PointF(13 * i, -1);
31                  g.DrawString(s, f, b, pf);
32              }            
33              Session["Captcha"] = sb.ToString();           
34              Response.ContentType = "image/pjpeg";
35              img.Save(Response.OutputStream, ImageFormat.Jpeg);            
36              b.Dispose();
37              g.Dispose();
38              img.Dispose();
39          }

 1 验证码样式四
 2  protected void Page_Load(object sender, EventArgs e)
 3          {            
 4              System.Random rand = new Random();
 5              int len = rand.Next(4, 6);
 6              char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
 7              System.Text.StringBuilder myStr = new System.Text.StringBuilder();
 8              for (int iCount = 0; iCount < len; iCount++)
 9              {
10                  myStr.Append(chars[rand.Next(chars.Length)]);
11              }
12              string text = myStr.ToString();            
13              this.Session["checkcode"] = text;
14              Size ImageSize = Size.Empty;
15              Font myFont = new Font("MS Sans Serif", 12);           
16              using (Bitmap bmp = new Bitmap(10, 10))
17              {
18                  using (Graphics g = Graphics.FromImage(bmp))
19                  {
20                      SizeF size = g.MeasureString(text, myFont, 5000);
21                      ImageSize.Width = (int)size.Width + 1;
22                      ImageSize.Height = (int)size.Height + 1;
23                  }
24              }            
25              using (Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height))
26              {                
27                  using (Graphics g = Graphics.FromImage(bmp))
28                  {
29                      g.Clear(Color.White);
30                      using (StringFormat f = new StringFormat())
31                      {
32                          f.Alignment = StringAlignment.Near;
33                          f.LineAlignment = StringAlignment.Center;
34                          f.FormatFlags = StringFormatFlags.NoWrap;
35                          g.DrawString(
36                              text,
37                              myFont,
38                              Brushes.Black,
39                              new RectangleF(
40                              0,
41                              0,
42                              ImageSize.Width,
43                              ImageSize.Height),
44                              f);
45                      }
46                  }                
47                  int num = ImageSize.Width * ImageSize.Height * 30 / 100;
48                  for (int iCount = 0; iCount < num; iCount++)
49                  {                    
50                      int x = rand.Next(ImageSize.Width);
51                      int y = rand.Next(ImageSize.Height);
52                      int r = rand.Next(255);
53                      int g = rand.Next(255);
54                      int b = rand.Next(255);
55                      Color c = Color.FromArgb(r, g, b);
56                      bmp.SetPixel(x, y, c);
57                  } 
58                  System.IO.MemoryStream ms = new System.IO.MemoryStream();
59                  bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
60                  this.Response.ContentType = "image/png";
61                  ms.WriteTo(this.Response.OutputStream);
62                  ms.Close();
63              }
64              myFont.Dispose();
65          }

  1 验证码样式五
  2      static string[] FontItems = new string[] 
  3      { 
  4           "Arial", 
  5            "Helvetica", 
  6            "Geneva", 
  7            "sans-serif", 
  8            "Verdana" 
  9      };
 10      static Brush[] BrushItems = new Brush[] 
 11      { 
 12            Brushes.OliveDrab, 
 13            Brushes.ForestGreen, 
 14            Brushes.DarkCyan, 
 15            Brushes.LightSlateGray, 
 16            Brushes.RoyalBlue, 
 17            Brushes.SlateBlue, 
 18            Brushes.DarkViolet, 
 19            Brushes.MediumVioletRed, 
 20            Brushes.IndianRed, 
 21            Brushes.Firebrick, 
 22            Brushes.Chocolate, 
 23            Brushes.Peru, 
 24            Brushes.Goldenrod 
 25      };
 26      static string[] BrushName = new string[] 
 27      { 
 28            "OliveDrab", 
 29            "ForestGreen", 
 30            "DarkCyan", 
 31            "LightSlateGray", 
 32            "RoyalBlue", 
 33            "SlateBlue", 
 34            "DarkViolet", 
 35            "MediumVioletRed", 
 36            "IndianRed", 
 37            "Firebrick", 
 38            "Chocolate", 
 39            "Peru", 
 40            "Goldenrod" 
 41      };
 42      private static Color BackColor = Color.White;
 43      private static Pen BorderColor = Pens.DarkGray;
 44      private static int Width = 50;
 45      private static int Height = 20;
 46      private Random _random;
 47      private string _code;
 48      private int _brushNameIndex;
 49  
 50      public void Page_Load(object sender, System.EventArgs e)
 51      {
 52          if (!IsPostBack)
 53          {            
 54              // TODO : initialize 
 55              this._random = new Random();
 56              this._code = GetRandomCode();
 57  
 58              // TODO : use Session["code"] save the VerifyCode 
 59              Session["code"] = this._code;
 60  
 61              // TODO : output Image 
 62              this.SetPageNoCache();
 63              this.OnPaint();
 64          }
 65      }
 66  
 67      //设置页面不被缓存 
 68      private void SetPageNoCache()
 69      {
 70          Response.Buffer = true;
 71          Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
 72          Response.Expires = 0;
 73          Response.CacheControl = "no-cache";
 74          Response.AppendHeader("Pragma", "No-Cache");
 75      }
 76      
 77      //取得一个 4 位的随机码 
 78      private string GetRandomCode()
 79      {
 80          return Guid.NewGuid().ToString().Substring(0, 4);
 81      }
 82  
 83      //随机取一个字体 
 84      private Font GetFont()
 85      {
 86          int fontIndex = _random.Next(0, FontItems.Length);
 87          FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
 88          return new Font(FontItems[fontIndex], 12, fontStyle);
 89      }
 90      
 91      //取一个字体的样式 
 92      private FontStyle GetFontStyle(int index)
 93      {
 94          switch (index)
 95          {
 96              case 0:
 97                  return FontStyle.Bold;
 98              case 1:
 99                  return FontStyle.Italic;
100              default:
101                  return FontStyle.Regular;
102          }
103      }
104      
105      //随机取一个笔刷 
106      private Brush GetBrush()
107      {
108          int brushIndex = _random.Next(0, BrushItems.Length);
109          _brushNameIndex = brushIndex;
110          return BrushItems[brushIndex];
111      }
112  
113  
114      //绘画事件 
115      private void OnPaint()
116      {
117          Bitmap objBitmap = null;
118          Graphics g = null;
119          try
120          {
121              objBitmap = new Bitmap(Width, Height);
122              g = Graphics.FromImage(objBitmap);
123              Paint_Background(g);
124              Paint_Text(g);
125              Paint_TextStain(objBitmap);
126              Paint_Border(g);
127              objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
128              Response.ContentType = "image/gif";
129          }
130          catch { }
131          finally
132          {
133              if (null != objBitmap)
134                  objBitmap.Dispose();
135              if (null != g)
136                  g.Dispose();
137          }
138      }
139  
140  
141      //绘画背景颜色 
142      private void Paint_Background(Graphics g)
143      {
144          g.Clear(BackColor);
145      }
146   
147      //绘画边框    
148      private void Paint_Border(Graphics g)
149      {
150          g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
151      }
152    
153      //绘画文字   
154      private void Paint_Text(Graphics g)
155      {
156          g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
157      }
158      
159      //绘画文字噪音点    
160      private void Paint_TextStain(Bitmap b)
161      {
162          for (int n = 0; n < 30; n++)
163          {
164              int x = _random.Next(Width);
165              int y = _random.Next(Height);
166              b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
167          }
168      }