里如何生成验证码asp.net
2006-01-22 14:10
399 查看
今天上网搜了搜怎么来生成验证码,例子好多的,看了一篇文章,有了一个大概的了解
我对 .net 的类 如BitMap 还不太了解,只能根据别人的来做了。他是用 VB .net 来做的,俺不会 VB.net ,只能用 C# 了。
首先新建一个 Web 项目,完成后新建一个Web 窗体,名为 Gif.aspx
建一方法
private void createPic(string str)
{
System.Drawing.Bitmap bmp;
System.Drawing.Graphics g;
System.IO.MemoryStream ms;
// 还不明白为什么要这样做,一会再看看教程
int picLen=str.Length*12;
bmp=new Bitmap(picLen,20);
g=System.Drawing.Graphics.FromImage(bmp);
g.DrawString(str,(new Font("Arial",12)),(new SolidBrush(Color.Blue)),1,1);
ms=new System.IO.MemoryStream();
bmp.Save(ms,ImageFormat.Png);
Response.ClearContent();
Response.ContentType="image/png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
bmp.Dispose();
Response.End();
}
做一个生成随机数的方法
private string createRandomiz(int n)
{
System.Random r=new Random();
string str="0,1,2,3,4,5,6,7,8,9,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
char[] a=new char[1];
a[0]=',';
string[] str1=str.Split(a);
string temp="";
for ( int i=0 ;i<n;i++)
{
temp=temp+str1[r.Next(35)].ToString();
}
return temp;
}
然后在 page_load 里调用就行了
private void Page_Load(object sender, System.EventArgs e)
{
string num=this.createRandomiz(4); // 生成 4 个随机数
Session["a"]=num; //这里用 Session 来保存
this.createPic(num);
}
在 WebForm1 里加一个 image 的 web 服务器控件 将 imageUrl 设为 gif.aspx
拖放一个 button 和一个 TextBox
在 Button 里写
if ( this.TextBox1.Text !=Session["a"].ToString())
{
Response.Write("error");
}
else
{
Response.Write("ok");
}
这样就完成了,继续看教程了
我对 .net 的类 如BitMap 还不太了解,只能根据别人的来做了。他是用 VB .net 来做的,俺不会 VB.net ,只能用 C# 了。
首先新建一个 Web 项目,完成后新建一个Web 窗体,名为 Gif.aspx
建一方法
private void createPic(string str)
{
System.Drawing.Bitmap bmp;
System.Drawing.Graphics g;
System.IO.MemoryStream ms;
// 还不明白为什么要这样做,一会再看看教程
int picLen=str.Length*12;
bmp=new Bitmap(picLen,20);
g=System.Drawing.Graphics.FromImage(bmp);
g.DrawString(str,(new Font("Arial",12)),(new SolidBrush(Color.Blue)),1,1);
ms=new System.IO.MemoryStream();
bmp.Save(ms,ImageFormat.Png);
Response.ClearContent();
Response.ContentType="image/png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
bmp.Dispose();
Response.End();
}
做一个生成随机数的方法
private string createRandomiz(int n)
{
System.Random r=new Random();
string str="0,1,2,3,4,5,6,7,8,9,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
char[] a=new char[1];
a[0]=',';
string[] str1=str.Split(a);
string temp="";
for ( int i=0 ;i<n;i++)
{
temp=temp+str1[r.Next(35)].ToString();
}
return temp;
}
然后在 page_load 里调用就行了
private void Page_Load(object sender, System.EventArgs e)
{
string num=this.createRandomiz(4); // 生成 4 个随机数
Session["a"]=num; //这里用 Session 来保存
this.createPic(num);
}
在 WebForm1 里加一个 image 的 web 服务器控件 将 imageUrl 设为 gif.aspx
拖放一个 button 和一个 TextBox
在 Button 里写
if ( this.TextBox1.Text !=Session["a"].ToString())
{
Response.Write("error");
}
else
{
Response.Write("ok");
}
这样就完成了,继续看教程了
相关文章推荐
- 详细说说如何生成验证码—ASP.NET细枝末节(4)
- ASP.NET中如何生成图形验证码
- 如何在asp.net中动态生成验证码
- 如何在asp.net中动态生成验证码
- ASP.NET中如何生成图形验证码
- 如何在asp.net中动态生成验证码(转)
- 如何在Asp.net窗体中使用验证码生成
- asp.net如何实现生成验证码的登录界面
- asp.net 生成验证码
- ASP.NET 生成验证码
- ASP.NET 生成验证码
- ASP.NET 实现登录界面(生成验证码)
- ASP.NET生成图形验证码的方法详解
- 9月19日----asp.net简单的验证码生成
- ASP.NET 实现登录界面(生成验证码)
- asp.net如何生成图片验证码(简单)
- 示例 - 如何在ASP.NET中应用Spider Studio生成的DLL?
- asp.net 生成漂亮的验证码
- Asp.net如何生成html静态页面
- Asp.Net 动态生成验证码