ASP.NET实现验证码

************************

实现验证码的类CreateValidateNumber.CS

*************************

public class CreateValidateNumber
?{

using System.Drawing;
using System.Drawing.Imaging;
using System.Web.UI;
using System.IO;

??public CreateValidateNumber()
??{

??}
??
??//生成图像函数
??public void ValidateCode(Page e,string VNum)
??{
???int Gheight=(int)(VNum.Length * 15);
???//gheight为图片宽度,根据字符长度自动更改图片宽度
???System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,25);
???Graphics g = Graphics.FromImage(Img);
???g.Clear(Color.White);
???
???//定义颜色
???Color[] c = {Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
???//定义字体????????
???string[] font = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体"};
???//随机输出噪点
???Random rand = new Random();
???for(int i=0;i<50;i++)
???{
????int x = rand.Next(Img.Width);
????int y = rand.Next(Img.Height);
????g.DrawRectangle(new Pen(Color.LightGray, 0),x,y,1,1);
???}
//???//随机输出直线
//???Pen blackPen = new Pen(Color.Black, 0);
//???for (int i=0;i<3;i++)
//???{
//????int y = rand.Next(Img.Height);
//????g.DrawLine(blackPen,0,y,Img.Width,y);
//???}
???//输出不同字体和颜色的验证码字符
???for(int i=0;i ???{
????int cindex = rand.Next(7);
????int findex = rand.Next(5);
????????????
????Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
????Brush b = new System.Drawing.SolidBrush(c[cindex]);
????int ii=4;
????if((i+1)%2==0)
????{
?????ii=2;
????}
????g.DrawString(VNum.Substring(i,1), f, b, 3+(i*12), ii);
???}
???//画一个边框
???g.DrawRectangle(new Pen(Color.Black,0),0,0,Img.Width-1,Img.Height-1);

???//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
???System.IO.MemoryStream ms=new System.IO.MemoryStream();
???Img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
???e.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
???e.Response.ContentType="image/gif";
???e.Response.BinaryWrite(ms.ToArray());
???g.Dispose();
???Img.Dispose();
???e.Response.End();
??}

??//生成随机数函数中从Vchar数组中随机抽取
??//字母区分大小写
??public string RndNum(int VcodeNum)
??{
???string Vchar = "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,W,X,Y,Z" ;
??
???string[] VcArray = Vchar.Split(',') ;
???string? VNum = "" ;//由于字符串很短,就不用StringBuilder了
???int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数

???//采用一个简单的算法以保证生成随机数的不同
???Random rand =new Random();
???for ( int i = 1 ; i < VcodeNum+1 ; i++ )
???{???
????if ( temp != -1)
????{
?????rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
????}???
????//int t =? rand.Next(35) ;
????int t=rand.Next(35);
????if (temp != -1 && temp == t)
????{
?????return RndNum( VcodeNum );
????}
????temp = t? ;
????VNum += VcArray[t];

???}
???return VNum ;
??}

//上面的方法也可以这么写

/*

public string RndNum(int VcodeNum)
??{
???string Vchar = "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" ;
??
???string[] VcArray = Vchar.Split(',') ;
???string VNum = "";

???//采用一个简单的算法以保证生成随机数的不同
???Random rand =new Random();
???for ( int i = 0 ; i < VcodeNum ; i++ )
???{???
???A: int t=rand.Next(36);
????
????string c = VcArray[t];

????int index = VNum.IndexOf(c,0);
????if(index<0)
?????????????????? VNum+=c;
????else
?????goto A;

???}
???return VNum ;
??}

*/
?}

***********************************

输出图片的页面

private void Page_Load(object sender, System.EventArgs e)
??{
???CreateValidateNumber create = new CreateValidateNumber();

???string VNum=create.RndNum(4); //4代表显示的是4位的验证字符串!

???Session["VNum"]=VNum;

???create.ValidateCode(this,VNum);//生成图片

??}

******************

在需要使用验证码的地方,放一个Image控件,将URL指向此页面即可

你可能感兴趣的:(asp.net)