文章出處
View Code
文章列表
摘自:http://blog.csdn.net/durongjian/article/details/4336380
一、創建ValidaeCode類庫工程:
1、創建ValidaeCode類庫工程,在[解決勝方案資源管理器]面板中,右鍵單擊[ValidateCode]節點,并選擇[屬性]命令。
2、單擊[屬性]命令,選中[應用程序],半分別把程序集名稱和默認命名空間兩個屬性值設為“ASPNETAJAXWeb.ValidateCode”和“ASPNETAJAXWeb.ValidateCode.Page”。
3、ValidateCode.cs:

using System; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; /// <summary> ///ValidateCode 的摘要說明 /// </summary> namespace ASPNETAJAXWeb.ValidateCode.Page { public class ValidateCode : System.Web.UI.Page { private const double IMAGELENGTHBASE = 12.5; private const int IMAGEHEIGTH = 22; private const int IMAGELINENUMBER = 25; private const int IMAGEPOINTNUMBER = 100; public static string VALIDATECODEKEY = "VALIDATECODEKEY"; private int length = 4;//驗證碼的數字個數 private string code = string.Empty; public ValidateCode() { // //TODO: 在此處添加構造函數邏輯 // } public int Length { get { return length; } set { length = value; } } public string Code { get { return Code; } } /// <summary> /// 創建隨機驗證碼 /// </summary> /// <param name="length"></param> /// <returns></returns> public string CreateCode(int length) { if (length <= 0) return string.Empty; Random random = new Random(); StringBuilder sbCode = new StringBuilder(); for (int i = 0; i < length; i++) { sbCode.Append(random.Next(0, 10)); } code = sbCode.ToString(); Session[VALIDATECODEKEY] = code; return code; } /// <summary> /// 創建驗證碼的圖片和驗證碼 /// </summary> /// <param name="code"></param> public void CreateValidateImage(string code) { if (string.IsNullOrEmpty(code) == true) return; Session[VALIDATECODEKEY] = code; Bitmap image = new Bitmap((int)Math.Ceiling((code.Length * IMAGELENGTHBASE)), IMAGEHEIGTH); Graphics g = Graphics.FromImage(image); ///隨機數生成器 Random random = new Random(); try { g.Clear(Color.White); ///繪制圖片的干擾線 int x1, x2, y1, y2; for (int i = 0; i < IMAGELINENUMBER; i++) { x1 = random.Next(image.Width); x2 = random.Next(image.Height); y1 = random.Next(image.Width); y2 = random.Next(image.Height); } ///繪制驗證碼 Font font = new Font("TaHoma", 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(code, font, brush, 2.0f, 2.0f); ///畫圖片的前景噪音點 int x, y; for (int i = 0; i < IMAGEPOINTNUMBER; i++) { x = random.Next(image.Width); 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 ms = new MemoryStream(); image.Save(ms, ImageFormat.Gif); /// Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } protected override void OnLoad(EventArgs e) { CreateValidateImage(length); } public void CreateValidateImage(int length) { code = CreateCode(length); CreateValidateImage(code); } } }
4、在另外一個ASP.NET應用程序中添加Web窗體頁ValidateCode.aspx,并刪除該頁面的代碼隱藏文件(ValidateCode.aspx.cs)。
5、刪除 ValidateCode.aspx頁面的HTML代碼,僅僅保留一行使用“@Page”指令的如下代碼:
<%@ Page Language="C#" AutoEventWireup="false" Inherits="ASPNETAJAXWeb.ValidateCode.Page.ValidateCode" %>
6、使用圖文驗證頁面:把 ValidateCode.aspx頁面復制到要使用該頁面的ASP.NET應用程序中,使用引用的方法把程序集添加到Bin目錄下,在使用驗證碼的頁面上添加一下Image控件,并將該控件的ImageUrl屬性的值設置為ValidateCode.aspx。
<asp:Image ID="Image1" runat="server" ImageUrl="~/ValidateCode.aspx" />
7、可通過判斷Session["VALIDATECODEKEY"]與用戶所輸入的值是否相等檢查用戶輸入的驗證碼是否正確。
文章列表
全站熱搜