3 验证码: 4
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
3using System;
4using System.Web;
5using System.Drawing;
6using System.Web.SessionState;
7
8public class ValidateCode : IHttpHandler, IRequiresSessionState
9 {
10 HttpContext context;
11public void ProcessRequest (HttpContext context1) {
12this.context = context1;
13 CreateCheckCodeImage(GenerateCheckCode());
14 }
16private string GenerateCheckCode()
17 {
18int number;
19char code;
20string checkCode = String.Empty;
21
22 System.Random random = new Random();
23
24for (int i = 0; i < 5; i++)
25 {
26 number = random.Next();
27
28if (number % 2 == 0)
29 code = (char)('0' + (char)(number % 10));
30else
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);
39return checkCode;
40 }
41
42private void CreateCheckCodeImage(string checkCode)
43 {
44if (checkCode == null || checkCode.Trim() == String.Empty) 45return;
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
50try
51 {
52//生成随机生成器
53 Random random = new Random();
54
55//清空图片背景色
56 g.Clear(Color.White);
58//画图片的背景噪音线
59for (int i = 0; i < 25; i++)
60 {
61int x1 = random.Next(image.Width);
62int x2 = random.Next(image.Width);
63int y1 = random.Next(image.Height);
64int 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//画图片的前景噪音点
74for (int i = 0; i < 100; i++)
75 {
76int x = random.Next(image.Width);
77int 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 }
91finally
92 {
93 g.Dispose();
94 image.Dispose();
95 }
96 }
97
98public bool IsReusable { 99get {
100return false; 101 }
102 }
103
104 }
验证码效果:。