.net處理程序生成隨機6位圖片驗證碼,大家參考使用吧
代碼如下: /// <summary> /// PicHandler1 的摘要說明 /// </summary> public class PicHandler1 : IHttpHandler, IRequiresSessionState { private string mCheckNo = string.Empty; protected ImgBuilder _ImgBuilder = new ImgBuilder(); protected VryImgGen _ImgBuilderNew = new VryImgGen(); private string _text = string.Empty; private string _font = "宋體"; private int _fontSize = 8; private int _padding = 2; public void ProcessRequest(HttpContext context) { mCheckNo = DisCheckNo(); context.Session["CheckCode"] = mCheckNo; this._ImgBuilder.FontSize = this._fontSize; this._ImgBuilder.Padding = this._padding; if (!string.IsNullOrEmpty(this._font)) { this._ImgBuilder.Fonts = new string[] { this._font }; } this._ImgBuilderNew.ChaosWight = 40; this._ImgBuilderNew.FontSize = 25; this._ImgBuilderNew.Padding = 3; System.Drawing.Bitmap image = this._ImgBuilderNew.CreateImage(mCheckNo); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/Jpeg"; context.Response.BinaryWrite(ms.ToArray()); context.Session["CheckCode"] = mCheckNo.ToString(); //如果沒有實現IRequiresSessionState,則這裡會出錯,也無法生成圖片 context.Response.End(); } //驗證碼生成 protected string DisCheckNo() { string hash = HashCode.GetNext(); string CheckNo = string.Empty; Random rd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 6; i++) { CheckNo += hash.Substring(rd.Next(1, hash.Length - 1), 1); } CheckNo = CheckNo.Replace("0", rd.Next(1, 9).ToString()); CheckNo = CheckNo.Replace("o", rd.Next(1, 9).ToString()); CheckNo = CheckNo.Replace("O", rd.Next(1, 9).ToString()); return CheckNo; } public bool IsReusable { get { return false; } } } 調用的類 代碼如下: using System; using System.Drawing; using System.Text; /// <summary> /// VryImgGen 的摘要說明 /// </summary> public class VryImgGen { public static string ChineseChars = String.Empty; /// <summary> /// 英文與數字串 /// </summary> protected static readonly string EnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public VryImgGen() { rnd = new Random(unchecked((int)DateTime.Now.Ticks)); } /// <summary> /// 全局隨機數生成器 /// </summary> private Random rnd; int length = 5; /// <summary> /// 驗證碼長度(默認6個驗證碼的長度) /// </summary> public int Length { get { return length; } set { length = value; } } int fontSize = 20; /// <summary> /// 驗證碼字體大小(為了顯示扭曲效果,默認30像素,可以自行修改) /// </summary> public int FontSize { get { return fontSize; } set { fontSize = value; } } int padding = 4; /// <summary> /// 邊框補(默認4像素) /// </summary> public int Padding { get { return padding; } set { padding = value; } } bool chaos = true; /// <summary> /// 是否輸出燥點(默認輸出) /// </summary> public bool Chaos { get { return chaos; } set { chaos = value; } } Color chaosColor = Color.LightGray; /// <summary> /// 輸出燥點的顏色(默認灰色) /// </summary> public Color ChaosColor { get { return chaosColor; } set { chaosColor = value; } } int chaosWight = 1; /// <summary> /// 輸出燥點的濃度 /// </summary> public int ChaosWight { get { return chaosWight; } set { chaosWight = value; } } Color backgroundColor = Color.White; /// <summary> /// 自定義背景色(默認白色) /// </summary> public Color BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; } } Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; /// <summary> /// 自定義隨機顏色數組 /// </summary> public Color[] Colors { get { return colors; } set { colors = value; } } string[] fonts = { "Arial", "Georgia" }; /// <summary> /// 自定義字體數組 /// </summary> public string[] Fonts