C#生成随机验证码例子:
前端:
1 <tr> 2 <td width="24%" height="26" align="center" valign="top"> 3 验证码:</td> 4 <td valign="top" width="37%" align="left"> 5 <input type="text" name="txtCode" id="txtvalidateCode"/><a href="javascript:void(0)" id="valiateCode"><img src="/ashx/ValidateCode.ashx?id=1" id="imgCode" /></a><span style="font-size:14px;color:red " id="userCodeMsg"></span></td> 6 </tr>
给验证码图片绑定单击事件:
$("#valiateCode").click(function () {
$("#imgCode").attr("src",$("#imgCode").attr("src")+1);
});
后台生成验证码图片代码:
ValidateCode.ashx
1 <%@ WebHandler Language="C#" Class="ValidateCode" %> 2 3 using System; 4 using System.Web; 5 using System.Drawing; 6 using System.Web.SessionState; 7 8 public class ValidateCode : IHttpHandler, IRequiresSessionState 9 { 10 HttpContext context; 11 public void ProcessRequest (HttpContext context1) { 12 this.context = context1; 13 CreateCheckCodeImage(GenerateCheckCode()); 14 } 15 16 private string GenerateCheckCode() 17 { 18 int number; 19 char code; 20 string checkCode = String.Empty; 21 22 System.Random random = new Random(); 23 24 for (int i = 0; i < 5; i++) 25 { 26 number = random.Next(); 27 28 if (number % 2 == 0) 29 code = (char)('0' + (char)(number % 10)); 30 else 31 code = (char)('0' + (char)(number % 10)); 32 //code = (char)('A' + (char)(number % 26)); 33 34 checkCode += code.ToString(); 35 } 36 37 //添加Session值 38 context.Session.Add("vCode", checkCode); 39 return checkCode; 40 } 41 42 private void CreateCheckCodeImage(string checkCode) 43 { 44 if (checkCode == null || checkCode.Trim() == String.Empty) 45 return; 46 47 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); 48 Graphics g = Graphics.FromImage(image); 49 50 try 51 { 52 //生成随机生成器 53 Random random = new Random(); 54 55 //清空图片背景色 56 g.Clear(Color.White); 57 58 //画图片的背景噪音线 59 for (int i = 0; i < 25; i++) 60 { 61 int x1 = random.Next(image.Width); 62 int x2 = random.Next(image.Width); 63 int y1 = random.Next(image.Height); 64 int y2 = random.Next(image.Height); 65 66 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 67 } 68 69 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 70 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); 71 g.DrawString(checkCode, font, brush, 2, 2); 72 73 //画图片的前景噪音点 74 for (int i = 0; i < 100; i++) 75 { 76 int x = random.Next(image.Width); 77 int y = random.Next(image.Height); 78 79 image.SetPixel(x, y, Color.FromArgb(random.Next())); 80 } 81 82 //画图片的边框线 83 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 84 85 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 86 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 87 context.Response.ClearContent(); 88 context.Response.ContentType = "image/Gif"; 89 context.Response.BinaryWrite(ms.ToArray()); 90 } 91 finally 92 { 93 g.Dispose(); 94 image.Dispose(); 95 } 96 } 97 98 public bool IsReusable { 99 get { 100 return false; 101 } 102 } 103 104 }