All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
理解C#,3.0新特性之Extension方法浅议_.net资料_编程技术
PHP进阶教程:实现网站的无限分类_php资料_编程技术
asp.net收集机器硬件信息的相关代码片断(cpu频率、磁盘可用空间、内存容量……)_[Asp.Net教程]
去除正文中的html标记,但是又想保留其中的<br>_[Asp.Net教程]
ASP.NET上传文件面面观_[Asp.Net教程]
ASP.NET实现自动返回上次请求页面小技巧_[Asp.Net教程]
asp.net(c#)如何把字符串转换成数组_[Asp.Net教程]
用javascript操纵GridView中CheckBox的两个常用技巧_[Asp.Net教程]
PHP,5.0,的变化与PHP,6.0,展望_php资料_编程技术
跟我学SQL:(五)创建和修改表格_mssql学习_编程技术
Reading number is top 10 pictures
NeedWallpaper5
So beauty, will let you spray blood2
Soong ching ling's former residence2
水晶头骨造型的酒瓶
Ashlynn Brooke a group sexy photo1
India's national beauty of the college students
Nikon microscopic photography of the first three
Absolutely shocked. National geographic 50 animal photographys3
中国文革时期的色情图片1
Gang rape
Download software ranking
Tram sex maniac 2 (H) rar bag14
Popkart Cracked versions Mobile phone games
星际争霸1.08硬盘免安装版
WebService在.NET中的实战应用教学视频 → 第3集
C#与.NET技术平台实战演练
Unix video tutorial5
Eclipse 4.2.1 For Win32
C语言教程TXT
Unix video tutorial1
Eclipse 4.2.2 For Win64
delv published in(发表于) 2014/1/8 7:04:25 Edit(编辑)
自己写的一个图形验证码页面(Asp.Net2.0通过)_[Asp.Net教程]

自己写的一个图形验证码页面(Asp.Net2.0通过)_[Asp.Net教程]

自己写的一个图形验证码页面(Asp.Net2.0通过)_[Asp.Net教程]

项目需要,要在首页登录界面添加一个图形验证码,赶时髦吧,网上一搜,特别多,找了几个,都不太满意。主要问题是大部分代码生成的图片宽度不唯一,页面布局不容易控制,其次是颜色单一,有些又过于抽象,不仔细看很容易弄错。针对特定的客户,我只需要“图片”长宽固定,颜色多样的数字图形验证码,借鉴网上的现有代码,自己操刀完成,以下是效果图:


原理不复杂,就是把网页当画布,运用各色画笔,在特定区域内画出数字,然后以特定格式(本例为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的相对路径即可


在需要验证的地方填入如下代码:
if (TextBox1.Text == Session["ValidateCode"].ToString())
{
TextBox1.Text = "正确!";
}
else
TextBox1.Text = "错误!";


  OK,基本搞定,总结一下:


  优点:


  1. 简单明了,适于简单运用
  2. 界面友好,图片长宽格式固定


  缺点:


  1. 如果有多个页面都需要此验证码,则会导致Session被其它页面重写的情况,可以考虑指定具体Session值为效验值


  2. 暂时只支持数字,不过更改GetRandomNumberString()中的代码可以实现指定字符机的随机字符串


  3. 页面刷新后验证码随之改变


  抛砖引玉,欢迎各位博友评点


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.