.Net Web EF

该代码示例展示了一个C#的HTTPHandler(Handler1),用于生成随机的五字符验证码图像。它首先定义了可用的字符集,然后随机选取5个字符组成验证码。验证码以小写形式存储在Session中,并通过创建一个图像,用不同的颜色和字体在图像上绘制验证码字符来显示。最后,响应以图像格式返回给客户端。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace _5_EF_Yanzheng
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler, IRequiresSessionState
    {
        //随机数对象
        private Random RandomSeed = new Random();
        public void ProcessRequest(HttpContext context)
        {
            string strWord = "23456789QWERTYUPASDFGHKXCVBNM";
            string NumStr = null;
            //获取随机5个字符
            for (int i = 0; i < 5; i++)
            {
                NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
            }
            context.Session["vcode"] = NumStr.ToLower();//将字符转成小写
            CreateImage(context, NumStr);
        }

        private void CreateImage(HttpContext context, string checkCode)
        {
            //编辑画板大小并编辑背景和清空画板内容
            int iwidth = (int)(checkCode.Length * 13);//当前字符数*13px
            Bitmap image = new Bitmap(iwidth, 22);//(宽,高)

            //画板
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);//填充白色


            //定义颜色集合
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            //定义字体集合
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic san MS", "Arial", "宋体" };

            //生产随机数对象
            Random rand = new Random();

            //文本框
            for (int i = 0; i < 50; i++)
            {
                //获取随机宽高 -- .Next表示获取正整数,为0时则返回最大值
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);

                //绘制颜色 位置 大小 的矩形框
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }


            //输出不同字体和颜色的验证码
            //checkCode.Length字符数
            for (int i = 0; i < checkCode.Length; i++)
            {
                //获取指定的数 -- .Next表示获取正整数,为0时则返回最大值
                int cindex = rand.Next(7);//从上定义的颜色集合(7)中获取随机一个
                int findex = rand.Next(5);//从上定义的字体集合(5)中获取随机一个

                //Font (字体类型(字体集合),大小,样式(加粗))
                Font f = new Font(font[findex], 10, FontStyle.Bold);//定义字体的大小样式
                Brush b = new SolidBrush(c[cindex]);//填充颜色(颜色集合)
                
                int li = 4;
                if ((i + 1) % 2 == 0)
                {
                    li = 2;

                } 
                //绘制字符   字符(位置,截取)位,字体,颜色,X轴,Y轴
                g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), li);

            }
            //画一个边框
            g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, image.Width - 1, image.Height - 1);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType = "image/gif";
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();//释放资源
            image.Dispose();//释放资源
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值