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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android spi读写不通,msm8909+android5.1.1 由BLSP3改为BLSP1后SPI读写速度慢问题解决

    android spi读写不通,msm8909+android5.1.1 由BLSP3改为BLSP1后SPI读写速度慢问题解决msm8909+android5.1.1由BLSP3改为BLSP1后SPI读写速度慢问题解决发现SPI的读写速度慢了不少,msm8909-cb03.dtsi文件相关内容如下:spi_0:spi@78b5000{/*BLSP1QUP1*/compatible=”qcom,spi-qup-v2″;#address-cells=<1>;#size-cells=<…

    2022年10月19日
    0
  • 数据连接建立流程_数据对接方式

    数据连接建立流程_数据对接方式数据连接建立流程

    2022年4月20日
    37
  • IOS 微信获取accesstoken 40029

    IOS 微信获取accesstoken 40029微信授权根据code获取accesstoken时报40029不合法的code在浏览器中是可以获取到token的 因为在客户端@”https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code”请求时重定向了,第二次的时候code参数差了导致

    2022年5月11日
    75
  • nginx路径匹配_url路径匹配

    nginx路径匹配_url路径匹配一、前言一般我们经常在访问网站时,通常会遇到输入某个页面的网址时,出现路由的转发,重定向等。可能访问的是一个网址,出来的时候就显示的是另外的地址。这种情况下,通常属于nginx的页面跳转。二、Nginx可提供的服务类型nginx做请求代理,提供多种服务1、php搭建的网站2、hexo创建的博客系统3、spring-boot&tomcat搭建的后台4、静态网页三、location匹配规则1、语法location可以把不同方式的请求,定位到不同的处理方式上.location

    2022年10月18日
    0
  • 格雷码与二进制的转换[通俗易懂]

    格雷码与二进制的转换[通俗易懂]一、什么是格雷码?格雷码,又叫循环二进制码或反射二进制码,格雷码是我们在工程中常会遇到的一种编码方式,它的基本的特点就是任意两个相邻的代码只有一位二进制数不同,这点在下面会详细讲解到。格雷码的基本特点就是任意两个相邻的代码只有一位二进制数不同,这点很重要。常用的二进制数与格雷码间的转换关系如下表:二、二进制格雷码与自然二进制码的互换1、二进制码转换成二进制格雷码  二进制码转换成二进制格雷码,

    2022年10月17日
    0
  • 微信公众平台开发入门教程——方倍工作室

    微信公众平台开发入门教程——方倍工作室微信公众平台开发入门教程——方倍工作室http://www.cnblogs.com/txw1958/p/wechat-tutorial.html

    2022年6月5日
    23

发表回复

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

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