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


相关推荐

  • 线程锁EnterCriticalSection和LeaveCriticalSection的用法

    线程锁EnterCriticalSection和LeaveCriticalSection的用法线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法注:使用结构CRITICAL_SECTION需加入头文件#include“afxmt.h”定义一个全局的锁CRITICAL_SECTION的实例  和一个静态全局变量CRITICAL_SECTIONcs;//可以理解为锁定一个资源staticintn_AddVal

    2022年9月16日
  • chmod命令详细用法

    chmod命令详细用法指令名称:chmod使用权限:所有使用者使用方式:chmod[-cfvR][–help][–version]modefile…说明:Linux/Unix的档案调用权限分为三级:档案拥有者、群组、其他。利用chmod可以藉以控制档案如何被他人所调用。参数:mode:权限设定字串,格式如下:[ugoa…][[±=][rwxX]…][,…],其中…

    2022年6月24日
  • 浙江8年级新增python_浙江八年级新增Python编程课程

    浙江8年级新增python_浙江八年级新增Python编程课程浙江消息,今年9月份开始的新学期,三到九年级信息技术课将同步替换新器材。其中,八年级将新增Python课程内容。新高一信息技术编程语言由VB替换为Python,大数据、人工智能、程序设计与算法按照教材规划五六年级开始接触。不得不说,在“少儿编程”这条路上,浙江省算是“死磕”到底了。早在2014年,浙江就发布了《浙江省深化高校考试招生制度综合改革试点方案》,方案提到:把信息技术(含编程)正式纳入高考…

    2022年5月17日
  • node.js获取RSS返回json

    node.js获取RSS返回json

    2021年9月12日
  • sha1的加密_sha加密算法

    sha1的加密_sha加密算法fromhashlibimportsha1#给password加密s1=sha1()#创建sha1加密对象s1.update(password.encode("utf-8&qu

    2022年8月6日
  • java局域网组建与维护题_局域网组建与维护习题(有答案).doc

    java局域网组建与维护题_局域网组建与维护习题(有答案).doc局域网组建与维护实用教程一、填空题计算机网络中常用的三种有线通信介质是双绞线、同轴电缆、光纤。局域网的英文缩写为LAN,城域网的英文缩写为_MAN_,广域网的英文缩写为WAN。计算机网络的功能主要表现在硬件资源共享、软件资源共享、数据资源共享。决定局域网特性的主要技术要素为媒体访问控制方式、拓扑结构、传输介质。计算机网络是现代_计算机_技术与通信技术密切组合的产物。局域网常用的拓扑结构有总…

    2022年10月25日

发表回复

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

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