用ashx一般处理程序创建验证码

 1 <%@ WebHandler Language="C#" Class="ValidateCode" %>
 2 using System;
 3 using System.Web;
 4 //下面是要使用到的命名空间
 5 using System.Drawing;
 6 using System.Drawing.Imaging;
 7 using System.Drawing.Drawing2D;
 8 using System.Web.SessionState;
 9 public class ValidateCode : IHttpHandler, IRequiresSessionState//继承这个接口
10 {
11     public void ProcessRequest(HttpContext context)
12     {
13         context.Response.ContentType = "image/jpeg";//验证码图片的格式
14         string str = CreateCode();
15         CreateStr(str);//将产生的随机数字,打印到画板上
16     }
17     Random random = new Random();//创建随机数对象
18     protected string CreateCode()
19     {
20         string randomStr = random.Next(1000, 10000).ToString();//产生一个四位的随机数
21         HttpContext.Current.Session["code"] = randomStr;//将产生的随机数赋值给Session
22         return randomStr;
23     }
24     protected void CreateStr(string code)
25     {
26         using (Bitmap map = new Bitmap(64, 30))//创建一张画板
27         {
28             using (Graphics gh = Graphics.FromImage(map))//为画板创建一个画笔
29             {
30                 gh.Clear(Color.White);//填充背景色
31                 //验证码边框
32                 gh.DrawRectangle(new Pen(Color.Silver), 0, 0, map.Width - 1, map.Height - 1);
33                 for (int i = 0; i < code.Length; i++)
34                 {
35                     Font font = new Font("黑体", 13.0f, FontStyle.Bold);//字体样式
36                     Point pointStrat = new Point(0, 0);//线性渐变色的起始点
37                     Point pointEnd = new Point(map.Width, map.Height);//线性渐变色的终止点
38                     //线性渐变色:从红色过度到绿色
39                     LinearGradientBrush brush = new LinearGradientBrush(pointStrat, pointEnd, Color.Red, Color.Green);
40                     PointF pointF = new PointF(13.0f * i, random.Next(10));//数字在画板的随机位置
41                     gh.DrawString(code.Substring(i, 1), font, brush, pointF);//设置随机数的属性
42                 }
43                 for (int i = 0; i < 5; i++)//干扰线
44                 {
45                     //干扰线的坐标
46                     int x1 = random.Next(map.Width);
47                     int x2 = random.Next(map.Height);
48                     int y1 = random.Next(map.Width);
49                     int y2 = random.Next(map.Height);
50                     Color color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));//干扰线的RGB颜色
51                     gh.DrawLine(new Pen(color), x1, x2, y1, y2);//设置干扰线的随机颜色和坐标
52                 }
53                 for (int i = 0; i < 100; i++)//干扰点
54                 {
55                     //干扰点的坐标
56                     int x = random.Next(map.Width);
57                     int y = random.Next(map.Height);
58                     Color color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));//干扰点的RGB颜色
59                     map.SetPixel(x, y, color);//设置干扰点的随机坐标和颜色
60                 }
61                 map.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);//保存验证码
62             }
63         }
64     }
65     public bool IsReusable
66     {
67         get
68         {
69             return false;
70         }
71     }
72 }

 

直接单击右击在浏览器中查看

验证码效果图:刷新页面验证码随机改变数字

posted on 2012-11-06 23:03  striver_hk  阅读(141)  评论(0编辑  收藏  举报