微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

各类验证码收集 整理笔记

样式一:

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);


 

样式二:

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,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),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)));            
             g.FillRectangle(b,width,height);
             Pen p = new Pen(Color.FromArgb(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,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)));                
                 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,Brushes.Black,new RectangleF(
                             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,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();
         }

感谢http://www.cnblogs.com/baiyuntian/archive/2011/11/10/2244334.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐