asp.net图形验证码 静态图形验证码之二
自从考虑图形验证码之后,
http://www.cnblogs.com/FayJack/articles/3063146.html
自己又突然有点想法....
就是这第二种....
<%@ WebHandler Language="C#" Class="CreateCode" %> using System; using System.Web; using System.Drawing; using System.Drawing.Imaging; using System.Web.SessionState; using System.Collections.Generic; using System.Text; using System.Drawing; public class CreateCode : IHttpHandler, IRequiresSessionState { /// <summary> /// 动态验证码 by Jack /// 传值type不为null时候 运算验证码 /// </summary> public void ProcessRequest(HttpContext context) { System.Web.HttpResponse response = context.Response; //验证码文字内容 string type=""; if (context.Request["type"] != null) type = context.Request["type"].ToString(); string checkCode = ""; if (string.IsNullOrEmpty(type)) { checkCode = CreateCheckCodeString(); context.Session.Add("gs_codeStr", checkCode); } else { string[] codes = CreateCheckCode(); checkCode = codes[0]; context.Session.Add("gs_codeStr", codes[1]); } int width = 80;//验证码图片宽度 int height = 22;//验证码图片高度 Font font = new Font("Arial", 12, FontStyle.Bold);//验证码字体 SolidBrush brush = new SolidBrush(Color.Black);//用于写验证码的画笔 Pen crosswise = new Pen(Color.Green, 0);//画横向干扰线的钢笔 Pen vertical = new Pen(Color.FromArgb(255, 100, 100, 100), 0);//画纵向干扰线的钢笔 Bitmap image = new Bitmap(width, height);//生成图像 Graphics g = Graphics.FromImage(image);//生成一个绘画面板(画布) g.Clear(ColorTranslator.FromHtml("#f0f0f0"));//用指定颜色填充画布 RectangleF rect = new RectangleF(10, 2, width, height);//定义文字的绘制矩形 浮动x ,y, 宽度, 高度, Random rand = new Random((int)DateTime.Now.Ticks);//生成干扰线的随机对象 for (int i = 0; i < 2; i++) { Point start = new Point(0, rand.Next(height)); Point end = new Point(width, rand.Next(height)); g.DrawLine(crosswise, start, end); } for (int i = 0; i < 4; i++) { Point start = new Point(rand.Next(width), 0); Point end = new Point(rand.Next(width), height); g.DrawLine(vertical, start, end); } g.DrawString(checkCode, font, brush, rect);//将验证码写到画布上 System.IO.MemoryStream ms = new System.IO.MemoryStream(); try { image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); response.ClearContent(); response.ContentType = "image/Gif"; response.BinaryWrite(ms.ToArray()); } finally { ms.Flush(); ms.Close(); ms.Dispose(); g.Dispose(); image.Dispose(); } } //定义字符串验证码 private string CreateCheckCodeString() { //定义用于验证码的字符数组 char[] AllCheckCodeArray ={ '1','2','3','4','5','6','7','8','9','A','B','C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', 'X','Y','Z'}; //定义验证码字符串 string randomcode = ""; Random rd = new Random(); //生成4位验证码字符串 for (int i = 0; i < 5; i++) randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)]; return randomcode; } //定义数字运算验证码 {1+1=? ,2} private string[] CreateCheckCode() { //定义用于验证码的字符数组 int[] AllCheckCodeArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //定义验证码字符串 string randomcode = "";//1+1=? int result = 0;//2 Random rd = new Random(); //生成4位验证码字符串 for (int i = 0; i < 5; i++) if (i != 1 && i != 4) { int curint = AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)]; if (i == 0 || i == 2) result += curint; if (i == 3) randomcode += "="; else randomcode += curint; } else { if (i == 1) randomcode += "+"; if (i == 4) randomcode += "?"; } return new string[] { randomcode, result.ToString() }; } public bool IsReusable { get { return false; } } }
相信看过我发过的第一篇验证码文字.再看这个之二遍有了新的理解
简单的计算验证码....
引用
<img src="CreateCode.ashx" />
<img src="CreateCode.ashx?type=1" />
验证过程同之前文章所说...