CreateImg.ashx.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

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

namespace 验证码
{
    /// <summary>
    /// CreateImg1 的摘要说明
    /// </summary>
    public class CreateImg1 : IHttpHandler,IRequiresSessionState
    {
        Random ran = new Random();
        public void ProcessRequest(HttpContext context)
        {
            
            string str = getRandomValidate(4);
            context.Session["check"] = str; //这一步是为了将验证码写入Session,进行验证,不能缺省,也可以使用cookie
            getImageValidate(context,str);
        }

        //得到随机字符串,长度自己定义
        private string getRandomValidate(int len)
        {
            int num;
            int tem;
            string rtuStr = "";
            for (int i = 0; i < len; i++)
            {
                num = ran.Next();
                if (num % 2 == 0)
                {
                    rtuStr += (char)('0' + (char)(num % 10));//生成数字
                }
                else
                {//一个十进制整数转换成char类型,其实就是这个整数对应的二进制数对应的ASCII码字符
                    //两个char类型相加,是将char转换成对应的二进制对应的十进制,然后再相加
                    rtuStr += (char)('A' + (char)(num % 26));//生成字母
                }
            }
            return rtuStr;
        }
        //生成图像
        private void getImageValidate(HttpContext context,string strValue)
        {
            int width = Convert.ToInt32(strValue.Length * 12);    //计算图像宽度
            Bitmap img = new Bitmap(width, 23);//创建一个位图对象
            Graphics gfc = Graphics.FromImage(img);//产生Graphics对象,进行画图
            gfc.Clear(Color.White);
            drawLine(gfc, img);//画随机线
            //写验证码,需要定义Font
            Font font = new Font("arial", 12, FontStyle.Bold);
            //创建画笔对象
            System.Drawing.Drawing2D.LinearGradientBrush brush =
                new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
            gfc.DrawString(strValue, font, brush, 3, 2);

后面还有 48 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。