驗證碼C#實現代碼

nbnb 9年前發布 | 6K 次閱讀 Perl 太原維信科技,java,

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>

 本文由用戶 nbnb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!