C# 登陆验证码工具类VerifyCode

C# 登陆验证码工具类VerifyCodeusingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;…

大家好,又见面了,我是你们的朋友全栈君。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 落地页测试代码
{
    public class VerifyCode
    {
        public byte[] GetVerifyCode()
        {
            int codeW = 80;
            int codeH = 30;
            int fontSize = 16;
            string chkCode = string.Empty;
            //颜色列表,用于验证码、噪线、噪点 
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
            //字体列表,用于验证码 
            string[] font = { "Times New Roman" };
            //验证码的字符集,去掉了一些容易混淆的字符 
            char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', '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、验证码MD5加密
            WebHelper.WriteSession("nfine_session_verifycode", Md5.md5(chkCode.ToLower(), 16)); 
            //创建画布
            Bitmap bmp = new Bitmap(codeW, codeH);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            //画噪线 
            for (int i = 0; i < 3; i++)
            {
                int x1 = rnd.Next(codeW);
                int y1 = rnd.Next(codeH);
                int x2 = rnd.Next(codeW);
                int y2 = rnd.Next(codeH);
                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, fontSize);
                Color clr = color[rnd.Next(color.Length)];
                g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
            }
            //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Png);
                return ms.ToArray();
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                g.Dispose();
                bmp.Dispose();
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/bin521/p/10569871.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159591.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js 取模 取余

    var i=10;var j=3;var mo=Math.floor(i/j);var yu=i%j;

    2022年4月9日
    42
  • Android 显示刷新机制、VSYNC和三重缓存机制

    Android 显示刷新机制、VSYNC和三重缓存机制Android显示刷新机制、VSYNC和三重缓存机制为了理解APP是如何进行渲染的,我们就必须了解手机硬件是如何工作的,也必须理解什么是VSYNC。首先,我们需要了解2个相关概念:刷新率(RefreshRate):代表了屏幕在一秒内刷新屏幕的次数,这取决于硬件的固定参数,例如60Hz。帧率(FrameRate):代表了GPU在一秒内绘制操作的帧数,例如30fps,60fps。GPU会获取图形数据进行渲染,然后硬件负责把渲染后的内容呈现到屏幕上,他们两者不停的进行协作。

    2022年5月21日
    42
  • PYTHON 全栈工程师「建议收藏」

    PYTHON 全栈工程师「建议收藏」FSDDevelopmentwithPython:全栈工程师,FSD(FullStackDeveloper),在PYTHONRestfulService软件开发上主要包括:明确开

    2022年7月6日
    21
  • 远程服务器虚拟显示器(Ubuntu 20.04 LTS)[通俗易懂]

    远程服务器虚拟显示器(Ubuntu 20.04 LTS)[通俗易懂]远程服务器虚拟显示器(Ubuntu20.04LTS)1.准备工作2.安装软件包3.修改配置文件4.卸载虚拟显示器1.准备工作安装sshsever并开启,确保虚拟显示器配置失败后,无法正常显示,仍可以通过ssh连接至服务器sudoapt-getinstallopenssh-server查看ssh服务已经开启,并可以远程ssh连接至服务器ps-e|grepssh测试远程连接2.安装软件包sudoapt-getinstall

    2022年8月21日
    5
  • 三级等保 服务器开启日志审计功能

    三级等保 服务器开启日志审计功能三级等保服务器开启日志审计功能

    2022年6月3日
    42
  • cf卡,mmc卡,sd卡,sm卡,xd卡,记忆棒的区别是什么?[通俗易懂]

    cf卡,mmc卡,sd卡,sm卡,xd卡,记忆棒的区别是什么?[通俗易懂]乐乐[学长] CF卡(CompactFlash)CF卡是1994年由SanDisk最先推出的。CF卡具有PCMCIA-ATA功能,并与之兼容;CF卡重量只有14g,仅纸板火柴般大小(43mm×36mm×3.3mm),是一种固态产品,也就是工作时没有运动部件。CF卡采用闪存(flash)技术,是一种稳定的存储解决方案,不

    2022年5月27日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号