首页 .Net 分享一个asp.net(ashx) 生成验证码图片

分享一个asp.net(ashx) 生成验证码图片

CodeImg.ashx
<%@ WebHandler Language=“C#” Class=“Enterprise.WebSite.App_Code.CodeImg” %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Web.SessionState;
namespace Enterprise.WebSite.App_Code
{
public class CodeImg : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        //随机验证码的数据
        string code = new Random().Next(1000, 9999).ToString();
        //将生成的验证码存储这session里,方便后台验证
        context.Session["ValidateCode"] = code;
        this.AddTextToImg(code, context);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    private void AddTextToImg(string text, HttpContext context)
    {

        string fileName = "bg_code.jpg";
        //判断背景验证码的背景图片是否存在
        if (!File.Exists(context.Server.MapPath(fileName)))
        {
            throw new FileNotFoundException("The file don't exist!");
        }
        if (text == string.Empty)
        {
            return;
        }
        System.Drawing.Image image = System.Drawing.Image.FromFile(context.Server.MapPath(fileName));
        int w = 80;
        int h = 30;
        Bitmap bitmap = new Bitmap(image, w, h);
        Graphics g = Graphics.FromImage(bitmap);
        //25.0f;             //字体大小
        float fontSize = w / (text.Length) * 1.22f;
        //文本的长度
        float textWidth = text.Length * fontSize;
        //下面定义一个矩形区域,以后在这个矩形里画上白底黑字
        float rectWidth = text.Length * fontSize * 1.6f;
        float rectHeight = fontSize * 1.1f;


        float rectX = 0;
        float rectY = (h - rectHeight) / 10f;

        //声明矩形域
        RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

        Font font = new Font("黑体", fontSize);   //定义字体
        Brush frontBrush = new SolidBrush(getFontColor(100));   //白笔刷,画文字用
        Brush backBrush = new SolidBrush(getFontColor(20));   //黑笔刷,画背景用
        for (int i = 0; i < 40; i++)
        {
            g.DrawLine(new Pen(backBrush, 1.6f), getPoint(i, w, h), getPoint(i + 1, w, h));
        }

        g.DrawString(text, font, frontBrush, textArea);
        MemoryStream ms = new MemoryStream();
        //保存为Jpg类型
        bitmap.Save(ms, ImageFormat.Jpeg);

        //输出处理后的图像,这里为了演示方便将图片显示在页面中了
        context.Response.Clear();
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(ms.ToArray());

        g.Dispose();
        bitmap.Dispose();
        image.Dispose();
    }

    private Point getPoint(int i, int w, int h)
    {
        Random r = new Random(i + DateTime.Now.Millisecond);
        int x = r.Next(-3 * w, 3 * w);
        int y = r.Next(-3 * h, 3 * h);
        return new Point(x, y);
    }

    private Color getFontColor(int seed)
    {
        Random r = new Random(seed + DateTime.Now.Millisecond);
        int i = r.Next(0, 9);
        Color[] color = new Color[]{
        Color.Violet,
        Color.YellowGreen,
        Color.Red,
        Color.RoyalBlue,
        Color.Purple,
        Color.SeaGreen,
        Color.SlateGray,
        Color.SteelBlue,
        Color.Teal,
        Color.YellowGreen
        };
        return color[i];
    }

}

}


特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。