惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
G
Google Developers Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
F
Full Disclosure
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Help Net Security
Help Net Security
The Hacker News
The Hacker News
IT之家
IT之家
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
博客园 - 聂微东
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Last Week in AI
Last Week in AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
N
News | PayPal Newsroom
A
About on SuperTechFans
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
K
Kaspersky official blog
The Cloudflare Blog
I
Intezer

博客园 - TomSun

sql语句优化 页面生存周期 SQL Server 存储过程的分页 利用鼠标中键缩放图片 - TomSun - 博客园 不间断连续图片滚动效果的制作方法 - TomSun - 博客园 IE问题解决方法汇总 Calendar的相关问题. 无法打开注册表关键字错误----修改方法 N级无刷新连动菜单 - TomSun - 博客园 JScript中正则表达函数的说明与应用 正则表达式语法 ASP生成柱型体,折线图,饼图源代码 - TomSun - 博客园 ASP必读[常见问题集] 常用SQL语句(不断更新) Javascript里类的思想(zz) - TomSun - 博客园 JS写的Cookie类 ASP调用系统ping命令 c#.net常用的小函数和方法集 XMLHTTP---介绍
ASP.net 验证码(C#)
TomSun · 2005-08-05 · via 博客园 - TomSun

Posted on 2005-08-05 09:10  TomSun  阅读(426)  评论()    收藏  举报

  1public class ValidateCode : System.Web.UI.Page
  2 {
  3  private void Page_Load(object sender, System.EventArgs e)
  4  {
  5   this.CreateCheckCodeImage(GenerateCheckCode());
  6  }

  7
  8  #region web 窗体设计器生成的代码
  9  override protected void OnInit(EventArgs e)
 10  {
 11   //
 12   // CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
 13   //
 14   InitializeComponent();
 15   base.OnInit(e);
 16  }

 17  
 18  /// <summary>
 19  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 20  /// 此方法的内容。
 21  /// </summary>

 22  private void InitializeComponent()
 23  {    
 24   this.Load += new System.EventHandler(this.Page_Load);
 25  }

 26  #endregion

 27
 28  private string GenerateCheckCode()
 29  {
 30   int number;
 31   char code;
 32   string checkCode = String.Empty;
 33
 34   System.Random random = new Random();
 35
 36   for(int i=0; i<5; i++)
 37   {
 38    number = random.Next();
 39
 40    if(number % 2 == 0)
 41     code = (char)('0' + (char)(number % 10));
 42    else
 43     code = (char)('A' + (char)(number % 26));
 44
 45    checkCode += code.ToString();
 46   }

 47
 48   Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
 49
 50   return checkCode;
 51  }

 52
 53  private void CreateCheckCodeImage(string checkCode)
 54  {
 55   if(checkCode == null || checkCode.Trim() == String.Empty)
 56    return;
 57
 58   System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
 59   Graphics g = Graphics.FromImage(image);
 60
 61   try
 62   {
 63    //生成随机生成器
 64    Random random = new Random();
 65
 66    //清空图片背景色
 67    g.Clear(Color.White);
 68
 69    //画图片的背景噪音线
 70    for(int i=0; i<25; i++)
 71    {
 72     int x1 = random.Next(image.Width);
 73     int x2 = random.Next(image.Width);
 74     int y1 = random.Next(image.Height);
 75     int y2 = random.Next(image.Height);
 76
 77     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 78    }

 79
 80    Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
 81    System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
 82    g.DrawString(checkCode, font, brush, 22);
 83
 84    //画图片的前景噪音点
 85    for(int i=0; i<100; i++)
 86    {
 87     int x = random.Next(image.Width);
 88     int y = random.Next(image.Height);
 89
 90     image.SetPixel(x, y, Color.FromArgb(random.Next()));
 91    }

 92
 93    //画图片的边框线
 94    g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
 95
 96    System.IO.MemoryStream ms = new System.IO.MemoryStream();
 97    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
 98    Response.ClearContent();
 99    Response.ContentType = "image/Gif";
100    Response.BinaryWrite(ms.ToArray());
101   }

102   finally
103   {
104    g.Dispose();
105    image.Dispose();
106   }

107  }