aps.net 图形验证码(转)

参考文章:

http://www.cnblogs.com/FayJack/articles/3063146.html

 

创建CreateCode.ashx文件:

<%@ 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
{
//验证码 by Jack
    public void ProcessRequest(HttpContext context)
    {
        System.Web.HttpResponse response = context.Response;
        //验证码文字内容
        string checkCode = CreateCheckCodeString();
        context.Session.Add("createcodeStr", checkCode);
         
        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(5, 2, width, height);//定义文字的绘制矩形
        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;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}

  

posted @ 2016-09-23 09:19  秒杀5S  阅读(224)  评论(0编辑  收藏  举报