生成验证码原理:产生随机字符,并将字符生成为图片,同时储存到Session里去,然后验证用户输入的内容是否与Session中的验证码相符即可。
效果图:用户可以点击切换验证码信息。
一般处理程序:CheckCodeHandler.cs
1 <%@ WebHandler Language="C#" Class="CheckCodeHandler" %> 2 3 using System; 4 using System.Web; 5 using System.Text; 6 using System.Drawing; 7 using System.Web.SessionState; 8 9 public class CheckCodeHandler : IHttpHandler,IRequiresSessionState 10 { 11 12 //产生验证码的字符集 13 public string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y"; 14 15 public void ProcessRequest (HttpContext context) { 16 string validateCode = CreateRandomCode(4); 17 context.Session["ValidateCode"] = validateCode;//将验证码保存到session中 18 CreateCodeImage(validateCode, context); 19 } 20 21 public bool IsReusable { 22 get { 23 return false; 24 } 25 } 26 27 /// <summary> 28 /// 生成验证码 29 /// </summary> 30 /// <param name="n">验证码个数</param> 31 /// <returns>验证码字符串</returns> 32 public string CreateRandomCode(int n) 33 { 34 string[] CharArray = charcode.Split(',');//将字符串转换为字符数组 35 string randomCode = ""; 36 int temp = -1; 37 38 Random rand = new Random(); 39 for (int i = 0; i < n; i++) 40 { 41 if (temp != -1) 42 { 43 rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); 44 } 45 int t = rand.Next(CharArray.Length - 1); 46 if (temp != -1 && temp == t) 47 { 48 return CreateRandomCode(n); 49 } 50 temp = t; 51 randomCode += CharArray[t]; 52 } 53 return randomCode; 54 } 55 56 public void CreateCodeImage(string checkCode, HttpContext context) 57 { 58 int iwidth = (int)(checkCode.Length * 13); 59 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); 60 Graphics g = Graphics.FromImage(image); 61 Font f = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold)); 62 63 // 前景色 64 Brush b = new System.Drawing.SolidBrush(Color.Black); 65 66 // 背景色 67 g.Clear(Color.White); 68 69 // 填充文字 70 g.DrawString(checkCode, f, b, 0, 1); 71 72 // 随机线条 73 74 Pen linePen = new Pen(Color.Gray, 0); 75 Random rand = new Random(); 76 77 for (int i = 0; i < 5; i++) 78 { 79 int x1 = rand.Next(image.Width); 80 int y1 = rand.Next(image.Height); 81 int x2 = rand.Next(image.Width); 82 int y2 = rand.Next(image.Height); 83 g.DrawLine(linePen, x1, y1, x2, y2); 84 } 85 86 // 随机点 87 for (int i = 0; i < 30; i++) 88 { 89 int x = rand.Next(image.Width); 90 int y = rand.Next(image.Height); 91 image.SetPixel(x, y, Color.Gray); 92 } 93 94 // 边框 95 g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1); 96 97 // 输出图片 98 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 99 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 100 context.Response.ClearContent(); 101 context.Response.ContentType = "image/jpeg"; 102 context.Response.BinaryWrite(ms.ToArray()); 103 g.Dispose(); 104 image.Dispose(); 105 } 106 }
封装成类库:ValidateNumber.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Drawing; 6 using System.Web.UI; 7 using System.Drawing.Drawing2D; 8 using System.IO; 9 using System.Drawing.Imaging; 10 11 /// <summary> 12 ///ValidateNumber 生成验证码 13 /// </summary> 14 public class ValidateNumber 15 { 16 //产生验证码的字符集 (易混淆的字符去掉) 17 private string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y"; 18 19 /// <summary> 20 /// 验证码的最大长度 21 /// </summary> 22 public int MaxLength 23 { 24 get { return 10; } 25 } 26 27 /// <summary> 28 /// 验证码的最小长度 29 /// </summary> 30 public int MinLength 31 { 32 get { return 1; } 33 } 34 35 /// <summary> 36 /// 生成验证码 37 /// </summary> 38 /// <param name="length">指定验证码的长度</param> 39 /// <returns></returns> 40 public string CreateValidateNumber(int length) 41 { 42 string[] CharArray = charcode.Split(',');//将字符串转换为字符数组 43 string randomCode = ""; 44 int temp = -1; 45 46 Random rand = new Random(); 47 for (int i = 0; i < length; i++) 48 { 49 if (temp != -1) 50 { 51 rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); 52 } 53 int t = rand.Next(CharArray.Length - 1); 54 if (temp != -1 && temp == t) 55 { 56 return CreateValidateNumber(length); 57 } 58 temp = t; 59 randomCode += CharArray[t]; 60 } 61 return randomCode; 62 } 63 64 /// <summary> 65 /// 创建验证码的图片 66 /// </summary> 67 /// <param name="context">context对象</param> 68 /// <param name="validateNum">验证码</param> 69 public void CreateValidateGraphic(HttpContext context,string validateNum) 70 { 71 int iwidth = (int)(validateNum.Length * 14); 72 Bitmap image = new Bitmap(iwidth, 22); 73 Graphics g = Graphics.FromImage(image); 74 try 75 { 76 //生成随机生成器 77 Random random = new Random(); 78 //清空图片背景色 79 g.Clear(Color.White); 80 //画图片的干扰线 81 for (int i = 0; i < 25; i++) 82 { 83 int x1 = random.Next(image.Width); 84 int x2 = random.Next(image.Width); 85 int y1 = random.Next(image.Height); 86 int y2 = random.Next(image.Height); 87 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 88 } 89 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 90 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 91 Color.Blue, Color.DarkRed, 1.2f, true); 92 g.DrawString(validateNum, font, brush, 3, 2); 93 //画图片的前景干扰点 94 for (int i = 0; i < 100; i++) 95 { 96 int x = random.Next(image.Width); 97 int y = random.Next(image.Height); 98 image.SetPixel(x, y, Color.FromArgb(random.Next())); 99 } 100 //画图片的边框线 101 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 102 //保存图片数据 103 MemoryStream stream = new MemoryStream(); 104 image.Save(stream, ImageFormat.Jpeg); 105 //输出图片 106 context.Response.Clear(); 107 context.Response.ContentType = "image/jpeg"; 108 context.Response.BinaryWrite(stream.ToArray()); 109 } 110 finally 111 { 112 g.Dispose(); 113 image.Dispose(); 114 } 115 } 116 /// <summary> 117 /// 得到验证码图片的长度 118 /// </summary> 119 /// <param name="validateNumLength">验证码的长度</param> 120 /// <returns></returns> 121 public static int GetImageWidth(int validateNumLength) 122 { 123 return (int)(validateNumLength * 14); 124 } 125 /// <summary> 126 /// 得到验证码图片的高度 127 /// </summary> 128 /// <returns></returns> 129 public static double GetImageHeight() 130 { 131 return 22; 132 } 133 }