当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  自己写的一个图形验证码页面(Asp.Net2.0通过)

 自己写的一个图形验证码页面(Asp.Net2.0通过)

点击次数:24 次 发布日期:2008-11-26 12:31:00 作者:源代码网
源代码网推荐      项目需要,要在首页登录界面添加一个图形验证码,赶时髦吧,网上一搜,特别多,找了几个,都不太满意。主要问题是大部分代码生成的图片宽度不唯一,页面布局不容易控制,其次是颜色单一,有些又过于抽象,不仔细看很容易弄错。针对特定的客户,我只需要“图片”长宽固定,颜色多样的数字图形验证码,借鉴网上的现有代码,自己操刀完成,以下是效果图:
源代码网推荐  
源代码网推荐  
源代码网推荐  原理不复杂,就是把网页当画布,运用各色画笔,在特定区域内画出数字,然后以特定格式(本例为PNG格式)发回客户端,在IE中显示为"图片",用于验证的字符串存于Session中。
源代码网推荐  
源代码网推荐  主要代码如下:
源代码网推荐  // 生成随机数字字符串
源代码网推荐  public string GetRandomNumberString(int int_NumberLength)
源代码网推荐  {
源代码网推荐   string str_Number = string.Empty;
源代码网推荐   Random theRandomNumber = new Random();
源代码网推荐  
源代码网推荐   for (int int_index = 0; int_index < int_NumberLength; int_index++)
源代码网推荐   str_Number += theRandomNumber.Next(10).ToString();
源代码网推荐  
源代码网推荐   return str_Number;
源代码网推荐  }
源代码网推荐  生成随机颜色
源代码网推荐  public Color GetRandomColor()
源代码网推荐  {
源代码网推荐   Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
源代码网推荐   // 对于C#的随机数,没什么好说的
源代码网推荐   System.Threading.Thread.Sleep(RandomNum_First.Next(50));
源代码网推荐   Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
源代码网推荐  
源代码网推荐   // 为了在白色背景上显示,尽量生成深色
源代码网推荐   int int_Red = RandomNum_First.Next(256);
源代码网推荐   int int_Green = RandomNum_Sencond.Next(256);
源代码网推荐   int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
源代码网推荐   int_Blue = (int_Blue > 255) ? 255 : int_Blue;
源代码网推荐  
源代码网推荐   return Color.FromArgb(int_Red, int_Green, int_Blue);
源代码网推荐  }
源代码网推荐  根据验证字符串生成最终图象
源代码网推荐  public void CreateImage(string str_ValidateCode)
源代码网推荐  {
源代码网推荐   int int_ImageWidth = str_ValidateCode.Length * 13;
源代码网推荐   Random newRandom = new Random();
源代码网推荐   // 图高20px
源代码网推荐   Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
源代码网推荐   Graphics theGraphics = Graphics.FromImage(theBitmap);
源代码网推荐   // 白色背景
源代码网推荐   theGraphics.Clear(Color.White);
源代码网推荐   // 灰色边框
源代码网推荐   theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
源代码网推荐  
源代码网推荐   // 10pt的字体
源代码网推荐   Font theFont = new Font("Arial", 10);
源代码网推荐  
源代码网推荐   for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
源代码网推荐   {
源代码网推荐   string str_char = str_ValidateCode.Substring(int_index, 1);
源代码网推荐   Brush newBrush = new SolidBrush(GetRandomColor());
源代码网推荐   Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
源代码网推荐   theGraphics.DrawString(str_char, theFont, newBrush, thePos);
源代码网推荐   }
源代码网推荐  
源代码网推荐   // 将生成的图片发回客户端
源代码网推荐   MemoryStream ms = new MemoryStream();
源代码网推荐   theBitmap.Save(ms, ImageFormat.Png);
源代码网推荐  
源代码网推荐   Response.ClearContent(); //需要输出图象信息 要修改HTTP头
源代码网推荐   Response.ContentType = "image/Png";
源代码网推荐   Response.BinaryWrite(ms.ToArray());
源代码网推荐   theGraphics.Dispose();
源代码网推荐   theBitmap.Dispose();
源代码网推荐   Response.End();
源代码网推荐  }
源代码网推荐  
源代码网推荐  最后在Page_Load中调用以上代码
源代码网推荐  
源代码网推荐  private void Page_Load(object sender, System.EventArgs e)
源代码网推荐  {
源代码网推荐   if(!IsPostBack)
源代码网推荐   {
源代码网推荐   // 4位数字的验证码
源代码网推荐   string str_ValidateCode = GetRandomNumberString(4);
源代码网推荐   // 用于验证的Session
源代码网推荐   Session["ValidateCode"] = str_ValidateCode;
源代码网推荐   CreateImage(str_ValidateCode);
源代码网推荐   }
源代码网推荐  }
源代码网推荐  使用的时候在页面中加入一个Image,将图片路径改为ValidateCode.aspx的相对路径即可
源代码网推荐  
源代码网推荐  <img src="http://img.sxsky.net/it/ValidateCode.aspx" />在需要验证的地方填入如下代码:
源代码网推荐  if (TextBox1.Text == Session["ValidateCode"].ToString())
源代码网推荐  {
源代码网推荐   TextBox1.Text = "正确!";
源代码网推荐  }
源代码网推荐  else
源代码网推荐   TextBox1.Text = "错误!";OK,基本搞定,总结一下:
源代码网推荐  优点:1. 简单明了,适于简单运用
源代码网推荐   2. 界面友好,图片长宽格式固定
源代码网推荐  缺点:1. 如果有多个页面都需要此验证码,则会导致Session被其它页面重写的情况,可以考虑指定具体Session值为效验值
源代码网推荐   2. 暂时只支持数字,不过更改GetRandomNumberString()中的代码可以实现指定字符机的随机字符串
源代码网推荐   3. 页面刷新后验证码随之改变
源代码网推荐  
源代码网推荐  抛砖引玉,欢迎各位博友评点
源代码网推荐  
源代码网推荐  http://www.cnblogs.com/nzai/archive/2006/07/27/validatecode.html
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华