C# 一般处理程序 ashx 写一个验证码

创建一个一般处理程序: Handler1.ashx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

namespace WebApplication2
{
    
    
    /// <summary>
    /// 用画图制作一个验证码
    /// </summary>
    public class Handler1 : IHttpHandler,IRequiresSessionState
    {
    
      //注意:需要加一个接口IRequiresSessionState,否则无法在页面显示

        public void ProcessRequest(HttpContext context)
        {
    
    
            Random random = new Random();  //实例化一个随机数
            context.Response.ContentType = "text/plain";
            //创建一个位图
            Bitmap bitmap = new Bitmap(60, 30);
            //根据新建的位图,创建一个画布
            //注意:这里Graphics类没有提供构造函数,所以不能直接建立Graphics类的对象
            Graphics g = Graphics.FromImage(bitmap);
            //设置画布背景颜色为白色
            g.Clear(Color.White);
            //设置边框
            g.DrawRectangle(new Pen(Color.Black), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
            //设置干扰点(噪音点)
            for (int i = 0; i < 70; i++)
            {
    
    
                int x = random.Next(bitmap.Width);
                int y = random.Next(bitmap.Height);
                bitmap.SetPixel(x, y, Color.Brown);
            }
            //设置干扰线(噪音线)
            for (int i = 0; i < 6; i++) //这里的噪音线数量不能太多,否则难以看清
            {
    
    
                int x1 = random.Next(bitmap.Width);
                int y1 = random.Next(bitmap.Height);//线的起点
                int x2 = random.Next(bitmap.Width);
                int y2 = random.Next(bitmap.Height);//线的终点
                g.DrawLine(new Pen(Color.Green), x1, y1, x2, y2);

            }
            //3.设置画图内容
            string s = "0123456789abcdefghjklmnopqrstuvwxyz"; //注:由于数字0和字母O不好辨认,所以可以选择去掉它们
            string vcode = "";
            for (int i = 0; i < 4; i++) //验证码长度为4个字符
            {
    
    
                int index = random.Next(s.Length);
                vcode += s[index];
            }       
            context.Session["vcode"] = vcode;

            //4.画图
            //设置符号渐变效果
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bitmap.Width, bitmap.Height), Color.Black, Color.Red, 1.2f);
            g.DrawString(vcode, new Font("宋体", 18), brush, 0, 0);            

            //5.保存
            //第一种方式:
            //string path=context.Request.MapPath("img");
            //bitmap.Save(path + "/" + vcode + ".jpg");
            //context.Response.Write("图片生产成功!");
            //第二种方式:
            bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        }

        public bool IsReusable
        {
    
    
            get{
    
      return false;  }
        }
    }
}

在HTML中进行使用

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>   
    <script>
    //使点击验证码时,实现刷新功能
        function getVcode() {
     
     
            var vcode = document.getElementById("vcode");
            vcode.src = "Handler1.ashx?i=" + Math.random();//只有后面加上随机数,点击验证码时才会刷新
            //方法2
            //var r="1";
            //vcode.src = "Handler1.ashx?i=" + r;
            //r += "1";
        }
    </script>
</head>
<body>
    <img src="Handler1.ashx" style="vertical-align:middle" onclick="getVcode()" id="vcode" /> 
</body>
</html>

效果图如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41802224/article/details/109102102