验证码C#实现代码

前端之家收集整理的这篇文章主要介绍了验证码C#实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

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,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),left,top);
            }
            //画一个边框边框颜色为Color.Gainsboro
            g.DrawRectangle(new Pen(Color.Gainsboro,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

    }
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的C#相关文章