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


相关推荐

  • IP地址的ABCDE类划分[通俗易懂]

    IP地址的ABCDE类划分[通俗易懂]1、0-—127。A类网络号码范围是0.0.0.0—127.0.0.0,用于128个网络。但网络不能近由0组成且127.0.0.0保留用于回路。剩下的126个网络,1到126,共有16777214个可能的主机地址(16777216减2)A类网络—主机——主机—主机0-1271270002、128—191。B

    2022年5月18日
    46
  • HDU 1548 A strange lift 搜索[通俗易懂]

    HDU 1548 A strange lift 搜索

    2022年2月1日
    36
  • nginx实现负载均衡几种方式_nginx如何负载均衡

    nginx实现负载均衡几种方式_nginx如何负载均衡Nginx负载均衡配置实例详解(转)负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦。负载均衡先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。测试环境由于没有服务器,所以本次测试直接host指定域名,然后在VMware里安装了三台CentOS。测试域名

    2025年6月3日
    3
  • 2021-09-09PHP json_encode中文乱码[通俗易懂]

    2021-09-09PHP json_encode中文乱码[通俗易懂]加参数限制就可以了。echojson_encode($res,JSON_UNESCAPED_UNICODE);

    2022年7月11日
    18
  • 线性代数的消元法_高斯消元法例题

    线性代数的消元法_高斯消元法例题1.消元的思想针对下面的方程,我们无法直接得到方程的解。$$\begin{alignedat}{2}&x\space\space&2&y\space=\space

    2022年8月2日
    12
  • 也谈AutoEventWireup「建议收藏」

    也谈AutoEventWireup「建议收藏」.aspx设置AutoEventWireup=false情况下,Page_Load方法没有办法成为Load事件的订阅者,我们必须手工进行相应的subscribe,不然我们就看不到输出的hello.<%@PageLanguage=”C#”AutoEventWireup=”false”CodeFile=”Default.aspx.cs”Inherits=”_D…

    2022年5月28日
    33

发表回复

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

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