C#產生隨機驗證碼的代碼



using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Text;
using  System.Drawing.Drawing2D;
using  System.Drawing;
using  System.IO;
using  System.Drawing.Imaging;
 
public  partial class ValidateCode : System.Web.UI.Page
{
     private  string code;
     private  const int ImageHeigth = 22;             //驗證碼圖片的高度
     private  const double ImageLengthBase = 12.5;    //驗證碼圖片中每個字符的寬度
     private  const int ImageLineNumber = 25;         //噪音線的數量
     private  const int ImagePointNumber = 100;       //噪點的數量
     private  int length;
     public  static string VALIDATECODEKEY;       //此處用來保存驗證碼到Session的Key的名稱
 
     //靜態構造函數初始化驗證碼在Session中的Key名稱
     static  ValidateCode()
     {
         VALIDATECODEKEY =  "VALIDATECODEKEY";
     }
 
     //設置驗證碼的長度和內容
     public  ValidateCode()
     {
         this .length = 4;
         this .code =  string.Empty;
     }
 
     ///
     /// 產生隨機的驗證碼並加入Session
     ///
     /// 驗證碼長度
     ///
     public  string CreateCode(int length)
     {
         if  (length <= 0)
         {
             return  string.Empty;
         }
         Random random =  new Random();
         StringBuilder builder =  new StringBuilder();
         //產生隨機的驗證碼並拼接起來
         for  (int i = 0; i < length; i++)
         {
             builder.Append(random.Next(0, 10));
         }
         this .code = builder.ToString();
         this .Session[VALIDATECODEKEY] =  this.code;
         return  this.code;
     }
 
     ///
     /// 根據長度產生驗證碼
     /// 並將驗證碼畫成圖片
     ///
     /// 驗證碼長度
     public  void CreateValidateImage(int length)
     {
         this .code =  this.CreateCode(length);
         this .CreateValidateImage( this .code);
     }
     ///
     /// 根據產生的驗證碼生成圖片
     ///
     /// 驗證碼
     public  void CreateValidateImage(string code)
     {
         if  (!string.IsNullOrEmpty(code))
         {
             this .Session[VALIDATECODEKEY] = code;
             //初始化位圖Bitmap對象,指定圖片對象的大小(寬,高)
             Bitmap image =  new Bitmap((int)Math.Ceiling((double)(code.Length * ImageLengthBase)), ImageHeigth);
             //初始化一塊畫布
             Graphics graphics = Graphics.FromImage(image);
             Random random =  new Random();
             try
             {
                 int  num5;
                 graphics.Clear(Color.White);
                 //繪制噪音線
                 for  (num5 = 0; num5 < ImageLineNumber; num5++)
                 {
                     int  num = random.Next(image.Width);
                     int  num3 = random.Next(image.Height);
                     int  num2 = random.Next(image.Width);
                     int  num4 = random.Next(image.Height);
                     graphics.DrawLine( new  Pen(Color.Silver), num, num3, num2, num4);
                 }
                 //驗證碼字體樣式
                 Font font =  new Font("Tahoma", 12, FontStyle.Italic | FontStyle.Bold);
                 LinearGradientBrush brush =  new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                 //繪制驗證碼到畫布
                 graphics.DrawString(code, font, brush, ( float )2, ( float )2);
                 //繪制噪點
                 for  (num5 = 0; num5 < ImagePointNumber; num5++)
                 {
                     int  x = random.Next(image.Width);
                     int  y = random.Next(image.Height);
                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
                 }
                 graphics.DrawRectangle( new  Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                 MemoryStream stream =  new MemoryStream();
                 //保存圖片
                 image.Save(stream, ImageFormat.Gif);
                 base .Response.ClearContent();
                 base .Response.ContentType =  "image/Gif";
                 base .Response.BinaryWrite(stream.ToArray());
             }
             finally
             {
                 graphics.Dispose();
                 image.Dispose();
             }
         }
     }
     protected  override void OnLoad(EventArgs e)
     {
         this .CreateValidateImage( this .length);
     }
 
     // Properties
     public  string Code
     {
         get
         {
             return  this.Code;
         }
     }
     public  int Length
     {
         get
         {
             return  this.length;
         }
         set
         {
             this .length = value;
         }
     }
}

再次申明非本人原創。可以到網上下載ASPNETAJAXWeb.ValidateCode.dll直接使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM