C#一般处理程序制作数字字母验证码
作者:韩明剑
时间:2021-05-12
浏览:1067
点赞:0

概述:本文是在一般处理程序(Handler或ashx)中实现的包含数字或字母的验证码实例代码,可拷贝直接使用。

代码:

一般处理程序(Handler)代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.SessionState;

namespace UNTDWS.Web.EasyUI.ashx.auth
{
    /// 
    /// 验证码
    /// 
    public class VerifyCodeHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string chkCode = string.Empty;

            //颜色列表,用于验证码、噪线、噪点
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Orange, Color.Brown, Color.DarkBlue };
            //字体列表,用于验证码
            string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //验证的字符集,去掉了一些容易混淆的字符
            char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random rnd = new Random();
            //生成验证码字符串
            for (int i = 0; i < 4; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            //写入session
            context.Session["ValidCode"] = chkCode;
            using (Bitmap bmp = new Bitmap(100, 40))
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.White);
                //画噪线
                for (int i = 0; i < 5; i++)
                {
                    int x1 = rnd.Next(100);
                    int y1 = rnd.Next(40);
                    int x2 = rnd.Next(100);
                    int y2 = rnd.Next(40);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
                }
                //画验证码字符串
                for (int i = 0; i < chkCode.Length; i++)
                {
                    string fnt = font[rnd.Next(font.Length)];
                    Font ft = new Font(fnt, 28);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 0, (float)0);
                }
                //画噪点
                for (int i = 0; i < 50; i++)
                {
                    int x = rnd.Next(bmp.Width);
                    int y = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    bmp.SetPixel(x, y, clr);
                }
                bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

前端代码

<input id="txtCode" name="txtCode" CssClass="login_input required" 
	style="width: 100px; height: 25px; border:1px solid #ccc" />
<img src="../VerifyCodeHandler.ashx" id="verifyCode" width="95" height="25" alt="点击切换验证码"
	title="点击切换验证码" style="cursor: pointer;" onclick="reloadCode()" />
<script type="text/javascript">
	function reloadCode() {
		document.getElementById('verifyCode').src = "../VerifyCodeHandler.ashx?" + Math.random();
	}
</script>

原创不易,转载请保留本站版权。