各类验证码收集

样式一:

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

样式二:

验证码样式二
  protected void Page_Load(object sender, EventArgs e)
         {
             CreateCheckCodeImage(GenerateCheckCode());
         }
 
         //随机生成验证码
         private string GenerateCheckCode()
         {
             int number;
             char code;
             string checkCode = String.Empty;
             System.Random random = new Random();
             for (int i = 0; i < 5; i++)
             {
                 number = random.Next();
                 if (number % 2 == 0)
                     code = (char)('0' + (char)(number % 10));
                 else
                     code = (char)('A' + (char)(number % 26));
                 checkCode += code.ToString();
             }
             Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
             return checkCode;
         }
 
         //生成验证码图片
         private void CreateCheckCodeImage(string checkCode)
         {
             if (checkCode == null || checkCode.Trim() == String.Empty)
                 return;
             System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
             Graphics g = Graphics.FromImage(image);
             try
             {
                 Random random = new Random();                       
                 g.Clear(Color.White);                
                 for (int i = 0; i < 25; i++)
                 {
                     int x1 = random.Next(image.Width);
                     int x2 = random.Next(image.Width);
                     int y1 = random.Next(image.Height);
                     int y2 = random.Next(image.Height);
                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                 }
 
                 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                 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);
                 g.DrawString(checkCode, font, brush, 2, 2);               
                 for (int i = 0; i < 100; i++)
                 {
                     int x = random.Next(image.Width);
                     int y = random.Next(image.Height);
 
                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
                 }                
                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                 Response.ClearContent();
                 Response.ContentType = "image/Gif";
                 Response.BinaryWrite(ms.ToArray());
             }
             finally
             {
                 g.Dispose();
                 image.Dispose();
             }
         }

样式三:

验证码样式三
  private static Random rnd = new Random();
 
         protected void Page_Load(object sender, EventArgs e)
         {            
             int width = 60;
             int height = 20;            
             Bitmap img = new Bitmap(width, height);           
             Graphics g = Graphics.FromImage(img);           
             Brush b;           
             b = new SolidBrush(Color.FromArgb(rnd.Next(200, 250), rnd.Next(200, 250), rnd.Next(200, 250)));            
             g.FillRectangle(b, 0, 0, width, height);
             Pen p = new Pen(Color.FromArgb(rnd.Next(160, 200), rnd.Next(160, 200), rnd.Next(160, 200)));
             for (int i = 0; i < 100; i++)
             {
                 int x1 = rnd.Next(width);
                 int y1 = rnd.Next(height);              
                 int x2 = x1 + rnd.Next(12);
                 int y2 = y1 + rnd.Next(12);               
                 g.DrawLine(p, x1, y1, x2, y2);
             }            
             Font f = new Font("Courier New", 16, FontStyle.Bold);            
             PointF pf;          
             StringBuilder sb = new StringBuilder();            
             for (int i = 0; i < 4; i++)
             {                
                 string s = rnd.Next(10).ToString();
                 sb.Append(s);
                 b = new SolidBrush(Color.FromArgb(rnd.Next(20, 130), rnd.Next(20, 130), rnd.Next(20, 130)));                
                 pf = new PointF(13 * i, -1);
                 g.DrawString(s, f, b, pf);
             }            
             Session["Captcha"] = sb.ToString();           
             Response.ContentType = "image/pjpeg";
             img.Save(Response.OutputStream, ImageFormat.Jpeg);            
             b.Dispose();
             g.Dispose();
             img.Dispose();
         }

样式四:

验证码样式四
 protected void Page_Load(object sender, EventArgs e)
         {            
             System.Random rand = new Random();
             int len = rand.Next(4, 6);
             char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
             System.Text.StringBuilder myStr = new System.Text.StringBuilder();
             for (int iCount = 0; iCount < len; iCount++)
             {
                 myStr.Append(chars[rand.Next(chars.Length)]);
             }
             string text = myStr.ToString();            
             this.Session["checkcode"] = text;
             Size ImageSize = Size.Empty;
             Font myFont = new Font("MS Sans Serif", 12);           
             using (Bitmap bmp = new Bitmap(10, 10))
             {
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     SizeF size = g.MeasureString(text, myFont, 5000);
                     ImageSize.Width = (int)size.Width + 1;
                     ImageSize.Height = (int)size.Height + 1;
                 }
             }            
             using (Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height))
             {                
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     g.Clear(Color.White);
                     using (StringFormat f = new StringFormat())
                     {
                         f.Alignment = StringAlignment.Near;
                         f.LineAlignment = StringAlignment.Center;
                         f.FormatFlags = StringFormatFlags.NoWrap;
                         g.DrawString(
                             text,
                             myFont,
                             Brushes.Black,
                             new RectangleF(
                             0,
                             0,
                             ImageSize.Width,
                             ImageSize.Height),
                             f);
                     }
                 }                
                 int num = ImageSize.Width * ImageSize.Height * 30 / 100;
                 for (int iCount = 0; iCount < num; iCount++)
                 {                    
                     int x = rand.Next(ImageSize.Width);
                     int y = rand.Next(ImageSize.Height);
                     int r = rand.Next(255);
                     int g = rand.Next(255);
                     int b = rand.Next(255);
                     Color c = Color.FromArgb(r, g, b);
                     bmp.SetPixel(x, y, c);
                 } 
                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
                 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                 this.Response.ContentType = "image/png";
                 ms.WriteTo(this.Response.OutputStream);
                 ms.Close();
             }
             myFont.Dispose();
         }

样式五:

验证码样式五
     static string[] FontItems = new string[] 
     { 
          "Arial", 
           "Helvetica", 
           "Geneva", 
           "sans-serif", 
           "Verdana" 
     };
     static Brush[] BrushItems = new Brush[] 
     { 
           Brushes.OliveDrab, 
           Brushes.ForestGreen, 
           Brushes.DarkCyan, 
           Brushes.LightSlateGray, 
           Brushes.RoyalBlue, 
           Brushes.SlateBlue, 
           Brushes.DarkViolet, 
           Brushes.MediumVioletRed, 
           Brushes.IndianRed, 
           Brushes.Firebrick, 
           Brushes.Chocolate, 
           Brushes.Peru, 
           Brushes.Goldenrod 
     };
     static string[] BrushName = new string[] 
     { 
           "OliveDrab", 
           "ForestGreen", 
           "DarkCyan", 
           "LightSlateGray", 
           "RoyalBlue", 
           "SlateBlue", 
           "DarkViolet", 
           "MediumVioletRed", 
           "IndianRed", 
           "Firebrick", 
           "Chocolate", 
           "Peru", 
           "Goldenrod" 
     };
     private static Color BackColor = Color.White;
     private static Pen BorderColor = Pens.DarkGray;
     private static int Width = 50;
     private static int Height = 20;
     private Random _random;
     private string _code;
     private int _brushNameIndex;
 
     public void Page_Load(object sender, System.EventArgs e)
     {
         if (!IsPostBack)
         {            
             // TODO : initialize 
             this._random = new Random();
             this._code = GetRandomCode();
 
             // TODO : use Session["code"] save the VerifyCode 
             Session["code"] = this._code;
 
             // TODO : output Image 
             this.SetPageNoCache();
             this.OnPaint();
         }
     }
 
     //设置页面不被缓存 
     private void SetPageNoCache()
     {
         Response.Buffer = true;
         Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
         Response.Expires = 0;
         Response.CacheControl = "no-cache";
         Response.AppendHeader("Pragma", "No-Cache");
     }
     
     //取得一个 4 位的随机码 
     private string GetRandomCode()
     {
         return Guid.NewGuid().ToString().Substring(0, 4);
     }
 
     //随机取一个字体 
     private Font GetFont()
     {
         int fontIndex = _random.Next(0, FontItems.Length);
         FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
         return new Font(FontItems[fontIndex], 12, fontStyle);
     }
     
     //取一个字体的样式 
     private FontStyle GetFontStyle(int index)
     {
         switch (index)
         {
             case 0:
                 return FontStyle.Bold;
             case 1:
                 return FontStyle.Italic;
             default:
                 return FontStyle.Regular;
         }
     }
     
     //随机取一个笔刷 
     private Brush GetBrush()
     {
         int brushIndex = _random.Next(0, BrushItems.Length);
         _brushNameIndex = brushIndex;
         return BrushItems[brushIndex];
     }
 
 
     //绘画事件 
     private void OnPaint()
     {
         Bitmap objBitmap = null;
         Graphics g = null;
         try
         {
             objBitmap = new Bitmap(Width, Height);
             g = Graphics.FromImage(objBitmap);
             Paint_Background(g);
             Paint_Text(g);
             Paint_TextStain(objBitmap);
             Paint_Border(g);
             objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
             Response.ContentType = "image/gif";
         }
         catch { }
         finally
         {
             if (null != objBitmap)
                 objBitmap.Dispose();
             if (null != g)
                 g.Dispose();
         }
     }
 
 
     //绘画背景颜色 
     private void Paint_Background(Graphics g)
     {
         g.Clear(BackColor);
     }
  
     //绘画边框    
     private void Paint_Border(Graphics g)
     {
         g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
     }
   
     //绘画文字   
     private void Paint_Text(Graphics g)
     {
         g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
     }
     
     //绘画文字噪音点    
     private void Paint_TextStain(Bitmap b)
     {
         for (int n = 0; n < 30; n++)
         {
             int x = _random.Next(Width);
             int y = _random.Next(Height);
             b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
         }
     }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值