分享一个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];
    }

}

}
如我这里直接将文件放到images 文件夹里面
在这里插入图片描述
运行代码,效果如下
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值