.NET 產生驗證碼的類

來源:互聯網
上載者:User

1.首先寫一個驗證碼的類

 ValidateNumber

 

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

/// <summary>
///ValidateNumber 的摘要說明
///生產驗證碼的類
/// </summary>
public class ValidateNumber
{
 public ValidateNumber()
 {
 
 }
    //生產驗證碼的長度
    public int MAxLength
    {
        get { return 10; }
    }
    public int MinLength
    {
        get { return 1; }
    }
    /// <summary>
    /// 產生驗證碼
    /// </summary>
    /// <param name="length">指定它的長度</param>
    /// <returns></returns>
    public string CreateValidateNumber(int length)
    {
        string validateMemberStr = "";
        //聲明整型數組來儲存隨機數
                int[] rand = new int[length];
     //////////////////////////////////////////////申明一個數組來取得隨機數      

        int[] validate = new int[length];
        //產生隨機數的序列值《按時間來的》

        int seek=unchecked((int)DateTime.Now.Ticks );
        //按照時間產生序列數
        Random seekRand = new Random(seek);

       //獲得一個範圍類的隨機數
        int bs = (int)seekRand.Next(0,Int32.MaxValue-length*10000 );

        #########再次申明數組來存放隨機數
        int[] ss = new int[length];
        for (int i = 0; i < length; i++ )
        {
            bs += 10000;
            ss[i] = bs;
        }
        //生產隨機數
        for (int i = 0; i < length; i++ )
        {
            Random rands = new Random(ss[i]);
            int pownum = 1 * (int)Math.Pow(10,length);
            rand[i] = rands.Next(pownum,Int32.MaxValue);
        }
        //抽取數字
        for (int i = 0; i < length; i++ )
        {
            string numStr = rand[i].ToString();
            int num = numStr.Length;
            Random randh = new Random();
            int numPosition = randh.Next(0,num-1);
            //給第二數組
            validate[i] = Int32.Parse(numStr.Substring(numPosition,1) );
        }
        //產生驗證碼
        for (int i = 0; i < length; i++)
        {
            //在抽取隨機數的數組中產生驗證碼
            validateMemberStr += validate[i].ToString();

        }
        return validateMemberStr;
    }
    //建立驗證碼圖片在page頁面中輸出
    public void CreateValidateGraphic(Page c, string validateNum)
    {
        Bitmap image = new Bitmap((int)Math.Ceiling(validateNum.Length*12.5),22);
        Graphics g = Graphics.FromImage(image);
        //產生隨即產生器
        Random random = new Random();
        //清空圖片背景色
        g.Clear(Color.White);
        //畫圖片的乾燥線
        for (int i = 0; i < 25; i++)
        {
            int x1 = random.Next(image.Width);
            int x2 = random.Next(image.Width);
            int y1 = random.Next(image.Height);
            int y2 = random.Next(image.Height);
            g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);

        }
        Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
         Color.Blue, Color.DarkRed, 1.2f, true);
        g.DrawString(validateNum, font, brush, 3, 2);
        //畫圖片的前景幹擾點
        for (int i = 0; i < 100; i++)
        {
            int x = random.Next(image.Width);
            int y = random.Next(image.Height);
            image.SetPixel(x, y, Color.FromArgb(random.Next()));
        }
        //畫圖片的邊框線
        g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
        //儲存圖片資料
        MemoryStream stream = new MemoryStream();
        image.Save(stream, ImageFormat.Jpeg);
        //輸出圖片
        c.Response.Clear();
        c.Response.ContentType = "image/jpeg";
        c.Response.BinaryWrite(stream.ToArray());
    }
    //驗證碼的長度
    public static int GetImageWidth(int validateNumLength)
    {
        return (int)(validateNumLength * 12.5);
    }
    public static double GetImageHeight()
    {
        return 22.5;
    }
}
//在建立一個頁面

在頁面中寫

             //執行個體化驗證碼的類

 ValidateNumber s = new ValidateNumber();

           //得到一個指定長度的驗證碼數字《建立驗證碼的數字》
        string str = s.CreateValidateNumber(4);

          //將驗證碼儲存在Seesion中
        Session["Vnmber"] = str;

          //建立驗證碼的圖片
       s.CreateValidateGraphic(this,str);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.