摘自: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"]与用户所输入的值是否相等检查用户输入的验证码是否正确。