首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> ASP.net 验证码(C#)
ASP.net 验证码(C#)


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-8-15 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

public class ValidateCode : System.Web.UI.Page
   {
   private void Page_Load(object sender, System.EventArgs e)
   {
   this.CreateCheckCodeImage(GenerateCheckCode());
   }
  
   #region web 窗体设计器生成的代码
   override protected void OnInit(EventArgs e)
   {
   //
   // CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
   }
  
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
   this.Load += new System.EventHandler(this.Page_Load);
   }
   #endregion
  
   private string GenerateCheckCode()
   {
   int number;
   char code;
   string checkCode = String.Empty;
  
   System.Random random = new Random();
  
   for(int i=0; i<5; i++)
   {
   number = random.Next();
  
   if(number % 2 == 0)
   code = (char)('0' + (char)(number % 10));
   else
   code = (char)('A' + (char)(number % 26));
  
   checkCode += code.ToString();
   }
  
   Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
  
   return checkCode;
   }
  
   private void CreateCheckCodeImage(string checkCode)
   {
   if(checkCode == null || checkCode.Trim() == String.Empty)
   return;
  
   System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
   Graphics g = Graphics.FromImage(image);
  
   try
   {
   //生成随机生成器
   Random random = new Random();
  
   //清空图片背景色
   g.Clear(Color.White);
  
   //画图片的背景噪音线
   for(int i=0; i<25; i++)
   {
   int x1 = random.Next(image.Width);
   int x2 = random.Next(image.Width);
   int y1 = random.Next(image.Height);
   int y2 = random.Next(image.Height);
  
   g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
   }
  
   Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
   System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
   g.DrawString(checkCode, font, brush, 2, 2);
  
   //画图片的前景噪音点
   for(int i=0; i<100; i++)
   {
   int x = random.Next(image.Width);
   int 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);
  
   System.IO.MemoryStream ms = new System.IO.MemoryStream();
   image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
   Response.ClearContent();
   Response.ContentType = "image/Gif";
   Response.BinaryWrite(ms.ToArray());
   }
   finally
   {
   g.Dispose();
   image.Dispose();
   }
   }
   }
http://www.cnblogs.com/niit007/archive/2006/08/13/475506.html
上一篇:如何用在ASP.NET中写入事件日志 下一篇:QQ新版月底发布 会员可添加1000个好友
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·ASP.NET中文件上传下载方法集合 2006-5-28
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·.Net下几种日志管理方法 2006-8-23
·支持正则表达式的UrlMapping(ASP.NET2.0) 2006-8-23
·ASP.NET2.0登陆控件的使用(常见的三种方法) 2006-8-23
·ASP.NET 2.0 X64的奇怪问题 2006-8-23
·ASP.NET 2.0 - Enter Key - Default 2006-8-23
·怎样从ASP.NET 2.0中读取连接字符串的设置 2006-8-23
·一个极有可能引发ExecutionEngineException的情 2006-8-23
·用gridview显示来自excel表格里的数据 2006-8-23
·ASP.NET服务器控件PleaseWaitButton[翻译] 2006-8-23
·ASP.NET 2.0 WebService中传递DataTable 2006-8-23
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: