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

Reading number is top 10 articles
深入ASP.NET数据绑定(上)_[Asp.Net教程]
ASP.NET,2.0,中的窗体身份验证_[Asp.Net教程]
小例子:ASP.NET定制简单的错误处理页面_[Asp.Net教程]
SQL循序渐进(17)JOIN子句_[SQL,Server教程]
SQL语句导入导出大全_mssql学习_编程技术
Access与SQLserver2000的数据互导_[SQL,Server教程]
ASP.NET获取汉字拼音的首字母_[Asp.Net教程]
轻松掌握Ajax.net系列教程八:使用AlwaysVisibleControlExtender_[Asp.Net教程]
PHP技巧:用PHP导出MySQL数据库内容为.sql文件_[PHP教程]
sql 2005 express 远程访问和sa密码的问题_[SQL Server教程]
Reading number is top 10 pictures
Summer is most suitable for young people to travel in China7
The little woman's bright wire3
如果没有好报,为什么要做好人?
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line1
美女浴室写真1
Born After 90 Beijing sports university campus flower photos3
NeedWallpaper4
2013中国四川省高考作文
A man's favorite things6
The hot big eye big breast beauty3
Download software ranking
Tram sex maniac 2 (H) rar bag12
The king of fighters 97(Mobile phone games-apk)
Call Of Duty5
Tram sex maniac 2 (H) rar bag1
Eclipse-CALMSANNY (second edition)
XML+Web+Service开发教程
Tram sex maniac 2 (H) rar bag2
linux初级教程
Tram sex maniac 2 (H) rar bag10
Boxer vs Yellow3
归海一刀 published in(发表于) 2014/1/30 0:59:52 Edit(编辑)
ASP.NET实现简单的验证码_[Asp.Net教程]

ASP.NET实现简单的验证码_[Asp.Net教程]

ASP.NET实现简单的验证码_[Asp.Net教程]

今天在公司忙完工作后总结验证码的实现,验证码在我们日常填写表单时很常见的。我从两个方面的实现.net下验证码的实现过程。希望能够给初学者带来帮助,也希望能起到抛砖引玉的作用:
一.通过自己写代码实现(牵涉到GDI+相关知识)
1.新建一个专门生成验证码的页面如:ValidateCode.aspx
2.在该页面的Page_Load事件里实现动态随机生成验证码图片:


private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//RndNum是一个自定义函数
//这里的数字4代表显示的是4位的验证字符串!
//string VNum=RndNum(4);
string VNum=this.GenerateRandom(4);
Session["VNum"]=VNum;
this.Validate_Code(VNum);


}
}private void Validate_Code(string VNum)
{
int Gheight=(int)(VNum.Length*11.5);
//Gheight为图片的宽度,根据字符串长度自动改变图片宽度
System.Drawing.Bitmap Img=new Bitmap(Gheight,20);
Graphics g=Graphics.FromImage(Img);
g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x,左上y)


System.IO.MemoryStream ms=new System.IO.MemoryStream();
Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent();//需要输出图象信息 要修改HTTP头
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}


public string GenerateRandom(int length)
{
System.Text.StringBuilder newRandom=new System.Text.StringBuilder(62);
Random rd=new Random();
for(int i=0;i {
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
private static char[] constant={'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
3.如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
//Image.ImageURL = "ValidateCode.aspx";
这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.ImageURL 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
二.验证码控件实现
用验证码实现就更简单了。通过下载点击下载就可以得到一个dll文件和相关的帮助文档.
通过把下载下来的dll文件添加到工具箱中变成自定义控件的过程拖到页面上来就可以实现验证码了。当然还可以设置有关属性。如字体的类型。表现形式,位置,长度都可以设置,具体可以参见下载下来的帮助文档。很全面。
下图是我用这种控件生成出来的效果图:


很酷吧!


来源:http://www.cnblogs.com/Charles2008







添加到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.