前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >验证码C#实现代码

验证码C#实现代码

原创
作者头像
代码伴一生
修改2021-11-02 14:17:24
5600
修改2021-11-02 14:17:24
举报
文章被收录于专栏:代码伴一生代码伴一生
代码语言:javascript
复制
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
//powered by http://outofmemory.cn/
namespace AppUtility.Tools
{
    /// <summary>
    /// VerifyCode 的摘要说明。
    /// </summary>
    public class VerifyCodeImgHelper
    {
        public VerifyCodeImgHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

    #region 验证码长度(默认4个验证码的长度)
    int length = 4;
    public int Length
    {
      get { return length; }
      set { length = value; }
    }
    #endregion

    #region 验证码字体大小默认11像素
    int fontSize = 11;
    public int FontSize
    {
        get { return fontSize; }
        set { fontSize = value; }
    }
    #endregion

    #region 边框补
    int padding = 1;
    public int Padding
    {
        get { return padding; }
        set { padding = value; }
    }
    #endregion

    #region 是否输出燥点(默认不输出)
    bool chaos = true;
    public bool Chaos
    {
        get { return chaos; }
        set { chaos = value; }
    }
    #endregion

    #region 输出燥点的颜色(默认灰色)
    Color chaosColor = Color.LightGray;
    public Color ChaosColor
    {
        get { return chaosColor; }
        set { chaosColor = value; }
    }
    #endregion

    #region 自定义背景色(默认白色)
    Color backgroundColor = Color.White;
    public Color BackgroundColor
    {
        get { return backgroundColor; }
        set { backgroundColor = value; }
    }
    #endregion

    #region 自定义随机颜色数组
    Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.DarkMagenta, Color.Brown, Color.DarkCyan, Color.Purple };
    public Color[] Colors
    {
        get { return colors; }
        set { colors = value; }
    }
    #endregion

    #region 自定义字体数组
    string[] fonts = { "Gautami","Tahoma","宋体" };
    public string[] Fonts
    {
        get { return fonts; }
        set { fonts = value; }
    }
    #endregion

    #region 自定义随机码字符串序列(使用逗号分隔)
    string codeSerial = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,2,3,4,5,s,t,u,v,w,x,y,z,6,7,8,A,B,D,E,F,G,H,K,M,N,Q";//
    public string CodeSerial
    {
        get { return codeSerial; }
        set { codeSerial = value; }
    }
    #endregion

    #region 生成校验码图片
    public Bitmap CreateImageCode(string code)
    {
        int fSize = FontSize;
        int fWidth = fSize + Padding;
        int imageWidth = 60;
        int imageHeight = 25;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
        Graphics g = Graphics.FromImage(image);
        g.Clear(BackgroundColor);
        Random rand = new Random();
        //给背景添加随机生成的燥点
        if (this.Chaos)
        {
            Pen pen = new Pen(ChaosColor, 0);
            int c = Length * 10;
            for (int i = 0; i < c; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(pen, x, y, 1, 1);
            }
        }
        int left = 0, top = 0;

        Font f;
        Brush b;
        int cindex, findex;
        //随机字体和颜色的验证码字符
        for (int i = 0; i < code.Length; i++)
        {
            cindex = rand.Next(Colors.Length - 1);
            findex = rand.Next(Fonts.Length - 1);

            f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
            b = new System.Drawing.SolidBrush(Colors[cindex]);
            if (i % 2 == 1)
            {
                top = 2;
            }
            else
            {
                top = 1;
            }

            left = i * fWidth;
            g.DrawString(code.Substring(i, 1), f, b, left, top);
        }
        //画一个边框边框颜色为Color.Gainsboro
        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
        g.Dispose();
        return image;
    }
    #endregion

    #region 将创建好的图片输出到页面
    public void CreateImageOnPage(string code, HttpContext context)
    {
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            using (Bitmap image = this.CreateImageCode(code))
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                context.Response.ClearContent();
                context.Response.ContentType = "image/Jpeg";
                context.Response.BinaryWrite(ms.GetBuffer());
                ms.Close();
            }
        }
    }
    #endregion

    #region 生成随机字符码
    public string CreateVerifyCode(int codeLen)
    {
        if (codeLen == 0)
        {
            codeLen = Length;
        }
        string[] arr = CodeSerial.Split(',');
        string code = "";
        int temp = -1;
        Random rand = new Random();
        for (int i = 0; i < codeLen; i++)
        {
            if (temp != -1)
            {
                rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
            }

            int t = rand.Next(arr.Length-1);

            while (temp == t)
            {
                t = rand.Next(arr.Length-1);
            }

            temp = t;
            code += arr[t];
        }

        return code;
    }

    #endregion

}

}</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档