首页 > 代码库 > ASP.NET验证码生成与识别

ASP.NET验证码生成与识别

  1 using System;  2 using System.Collections.Generic;  3 using System.Drawing;  4 using System.Drawing.Drawing2D;  5 using System.Text;  6 using System.Web;  7 using System.Web.SessionState;  8   9 namespace 验证码的生成与识别 10 { 11     /// <summary> 12     /// ValidCodeHandler 的摘要说明 13     /// </summary> 14     public class ValidCodeHandler : IHttpHandler,IRequiresSessionState 15     { 16  17         public void ProcessRequest(HttpContext context) 18         { 19             //设置输出流的MIME类型 20             //context.Response.ContentType = "image/png";       21  22             string strValidCode = GetValidCode(5);                                                  // 产生5位随机字符 23  24             //如果要使用context.Session,需要添加命名空间using System.Web.SessionState;,然后再继承IRequiresSessionState接口 25             context.Session["ValidCode"] = strValidCode;                                            //将字符串保存到Session中,以便需要时进行验证 26              27             Bitmap image = new Bitmap(120, 40);                                                     //定义宽120像素,高30像素的数据定义的图像对象 28             Graphics g = Graphics.FromImage(image);                                                 //绘制图片 29             try 30             { 31  32                 Random random = new Random();                                                       //生成随机对象 33                 g.Clear(Color.White);                                                               //清除图片背景色 34  35                 for (int i = 0; i < 25; i++)                                               // 随机产生图片的背景噪线 36                 { 37                     int x1 = random.Next(image.Width); 38                     int x2 = random.Next(image.Width); 39                     int y1 = random.Next(image.Height); 40                     int y2 = random.Next(image.Height); 41                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 42                 } 43  44                 Font font = new System.Drawing.Font("微软雅黑", 20, (System.Drawing.FontStyle.Bold));  //设置图片字体风格 45                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 3, true);//设置画笔类型 46                 g.DrawString(strValidCode, font, brush, 5, 2);                            //绘制随机字符 47  48  49                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);    //绘制图片的前景噪点 50                  51                 System.IO.MemoryStream ms = new System.IO.MemoryStream();                           //建立存储区为内存的流 52                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);                              //将图像对象储存为内存流 53                 context.Response.ClearContent();                                                    //清除当前缓冲区流中的所有内容 54  55                 context.Response.ContentType = "image/png"; 56                 //context.Response.ContentType = "image/Gif";                                         //设置输出流的MIME类型 57                 context.Response.BinaryWrite(ms.ToArray());                                         //将内存流写入到输出流 58             } 59             finally 60             { 61                 g.Dispose(); 62                 image.Dispose(); 63             } 64         } 65  66         /// <summary> 67         /// 生成随机字符串 68         /// </summary> 69         /// <param name="num">随机字符的个数</param> 70         /// <returns>返回随机产生的字符串</returns> 71         private string GetValidCode(int num) 72         { 73             //定义一个允许的字符组成的字符串 74             string strRandomCode = "ABCD1EF2GH3IJ4KL5MN6P7QR8ST9UVWXYZ";                                //定义要随机抽取的字符串 75             //char[] chaStr = strRandomCode.ToCharArray();                                                //将定义的字符串转成字符数组 76             StringBuilder sbValidCode = new StringBuilder();                                            //定义StringBuilder对象用于存放验证码 77             //随机数生成器,用于随机产生验证码中字符 78             Random rnd = new Random();                                                                   //随机函数,随机抽取字符 79             for (int i = 0; i < num; i++) 80             { 81                 //随机获取一个字符 82                 char a = strRandomCode[rnd.Next(0, strRandomCode.Length)]; 83                 // 84                 sbValidCode.Append(a); 85                 //以strRandomCode的长度产生随机位置并截取该位置的字符添加到StringBuilder对象中 86                 //sbValidCode.Append(strRandomCode.Substring(rnd.Next(0, strRandomCode.Length), 1)); 87             } 88             return sbValidCode.ToString(); 89         } 90  91  92  93         public bool IsReusable 94         { 95             get 96             { 97                 return false; 98             } 99         }100     }101 }

 

ASP.NET验证码生成与识别