验证码

1 搞一个模块,编译成DLL
  

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;

  /// <summary>
  /// 获取或设置验证码长度,默认值为4。
  /// </summary>
  public int Length
  {
   get
   {
    return length;
   }
   set
   {
    length = value;
   }
  }

  /// <summary>
  /// 获取验证码
  /// </summary>
  public string Code
  {
   get
   {
    return Code;
   }
  }


  public ValidateCode()
  {

  }

  protected override void OnLoad(EventArgs e)
  {
   CreateValidateImage(length);
  }

  /// <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));
   }
   ///保存验证码到Session对象中
   code = sbCode.ToString();
   Session[VALIDATECODEKEY] = code;
   return code;
  }

  /// <summary>
  /// 创建验证码的图片和验证码
  /// </summary>
  /// <param name="length">验证码的长度</param>
  public void CreateValidateImage(int length)
  {   ///创建验证码
   code = CreateCode(length);
   ///创建验证码的图片
   CreateValidateImage(code);
  }

  /// <summary>
  /// 创建验证码的图片和验证码
  /// </summary>
  /// <param name="code">验证码</param>
  public void CreateValidateImage(string code)
  {
   if(string.IsNullOrEmpty(code) == true) return;
   ///保存验证码到Session对象中
   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);
     y1 = random.Next(image.Height);
     x2 = random.Next(image.Width);
     y2 = random.Next(image.Height);
     ///绘制干扰线
     g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
    }

    ///绘制验证码
    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();
   }
  }
 }
}

 2  搞一个validatecode.asp页
  <%@ Page Language="C#" AutoEventWireup="false"  Inherits="ASPNETAJAXWeb.ValidateCode.Page.ValidateCode" %>
3 在要用验证码的地方,引用编译好的DLL
4 要用的地方
   <tr bgcolor="white">      
     <td>验 证 码:</td>
     <td>
      <asp:TextBox ID="tbCode" runat="server" SkinID="tbSkin" Width="80px"></asp:TextBox>
      <asp:Image ID="imgCode" runat="server" ImageUrl = "~/ValidateCode.aspx" />           
     </td>
    </tr> 
5 逻辑代码
   //判断是否创建了验证吗
  if(Session[ValidateCode.VALIDATECODEKEY] == null) return;
  ///验证验证码是否相等
  if(tbCode.Text != Session[ValidateCode.VALIDATECODEKEY].ToString())
  {
   lbMessage.Text = "验证码输入错误,请重新输入。";
   return;
  }

你可能感兴趣的:(session,server,String,image,Random,textbox)