使用GDI技术创建ASP.NET验证码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Drawing;
  6 using System.IO;
  7 using System.Text;
  8 using System.Text.RegularExpressions;
  9 using System.Drawing.Drawing2D;
 10 using System.Drawing.Imaging;
 11 using System.Web.SessionState;
 12 namespace yzm
 13 {
 14     /// <summary>
 15     /// yzm 的摘要说明
 16     /// </summary>
 17     public class yzm : IHttpHandler,IRequiresSessionState
 18     {
 19         private const string yzmName = "yzm";
 20 
 21         public void ProcessRequest(HttpContext context)
 22         {
 23 
 24             //表头需要修改 MIME类型为:image/jpeg,因为此页面要以图片流的形式响应给客户端
 25             context.Response.ContentType = "image/jpeg";
 26 
 27             
 28 
 29             //获取验证码的内容
 30             string code = CreateCode(context, 4);
 31 
 32 
 33             //将内容保存到Seesion,在效验时取客户端提交的值在服务器里与此Session内容验证
 34             context.Session[yzmName] = code;
 35 
 36 
 37 
 38 
 39             /*
 40              绘制样式
 41              */
 42             using (Bitmap map =new Bitmap (120,50)) //创建位图对象,构造函数指定宽、高
 43             using(Graphics grap=Graphics.FromImage(map)) //创建绘图的图纸
 44 
 45             {
 46                 grap.Clear(Color.White);  //让图纸为白色
 47 
 48                 /*创建一个矩形对象
 49                     构造函数(x坐标,y坐标,宽,高)
 50                  * 该参数的填写,其实是填满了整个图纸
 51                  */
 52                 Rectangle rec = new Rectangle(0,0,map.Width,map.Height);
 53 
 54                 /*创建了一个画线性,并且颜色是渐变的一个画笔对象
 55                  构造函数:   画在矩形对象上,起始颜色,结束颜色,角度,是否比例
 56                  */
 57                 using (LinearGradientBrush brush = new LinearGradientBrush(rec,Color.Blue,Color.Red,1.2f,true))
 58                 using(  Font font = new Font("隶书", 20, FontStyle.Strikeout))   //为验证码指定字体
 59                 {
 60                     grap.DrawString(code, font, brush, 3, 8);  //画到图纸上,传入相应对象:内容、字体、画笔、坐标
 61                 }
 62 
 63                 //画一些随机的直线,增加验证码识别度
 64                 Random random = new Random();
 65                 for (int i = 0; i < 18; i++)
 66                 {
 67                     //第一个点
 68                     int x1 = random.Next(map.Width);
 69                     int y1 = random.Next(map.Height);
 70 
 71                     //第二个点
 72                     int x2 = random.Next(map.Width);        
 73                     int y2 = random.Next(map.Height);
 74 
 75                     /*在画纸上绘画线
 76                      * 构造函数:
 77                      * pen:创建画笔(画笔颜色,宽度)
 78                        x1, y1, x2, y2 随机生成 两个坐标点连成一条线
 79                      */
 80                     grap.DrawLine(new Pen(Color.FromArgb(random.Next()), 3), x1, y1, x2, y2);
 81 
 82                 }
 83 
 84 
 85                 //保存到响应对象的流中 ,返回给客户端一个jpg格式图片
 86                 map.Save(context.Response.OutputStream, ImageFormat.Jpeg);
 87             
 88 
 89 
 90 
 91             } //--end
 92 
 93 
 94 
 95         }
 96 
 97 
 98 
 99         /// <summary>
100         /// 用txt里的汉字作为验证码数据源
101         /// </summary>
102         private string Get_yzm_DataSource(HttpContext context)
103         {
104             StringBuilder sb = new StringBuilder();
105 
106             string path = context.Server.MapPath(@"~yzmDataSource.txt");
107 
108             using (Stream fs = new FileStream(path, FileMode.Open))
109             using (StreamReader read = new StreamReader(fs, Encoding.UTF8))
110             {
111                 string len;
112                 while ((len = read.ReadLine()) != null)
113                 {
114                     Regex rg = new Regex("[\W\s]+");
115                     sb.Append(rg.Replace(len, ""));
116                 }
117             }
118 
119 
120             return sb.ToString();
121         }
122 
123 
124         /// <summary>
125         /// 随机生成,指定个数的验证码内容
126         /// </summary>
127         /// <returns></returns>
128         private string CreateCode(HttpContext context, int length)
129         {
130             string str = Get_yzm_DataSource(context);
131 
132             Random ran = new Random();
133 
134 
135 
136             string code = "";
137             for (int i = 0; i < length; i++)
138             {
139                 int sjIndex = ran.Next(0, str.Length);
140                 code += str[sjIndex];
141             }
142 
143             return code;
144         }
145 
146 
147         public bool IsReusable
148         {
149             get
150             {
151                 return false;
152             }
153         }
154     }
155 }
原文地址:https://www.cnblogs.com/green-jcx/p/5807377.html