各类验证码收集 整理笔记

来源:互联网 发布:cnc编程招聘 编辑:程序博客网 时间:2024/06/06 08:25

样式一:

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, 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();         }

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

原创粉丝点击