验证码ASP.NET
验证码的:
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
string tmp = RndNum(4);
HttpCookie a = new HttpCookie("ImageV",tmp);
Response.Cookies.Add(a);
this.ValidateCode(tmp);
}
}
private void ValidateCode(string VNum)
{
Bitmap Img=null;
Graphics g=null;
MemoryStream ms=null;
int gheight=VNum.Length*12;
Img=new Bitmap(gheight,25);
g=Graphics.FromImage(Img);
//背景颜色
g.Clear(Color.White);
//文字字体
Font f=new Font("Arial Black",10);
//文字颜色
SolidBrush s=new SolidBrush(Color.Black);
g.DrawString(VNum,f,s,3,3);
ms=new MemoryStream();
Img.Save(ms,ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType="image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private 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(new Char [] {','});
string VNum="";
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);
if(temp!=-1&&temp==t)
{
return RndNum(VcodeNum);
}
temp=t;
VNum+=VcArray[t];
}
return VNum;
}
============================================================
你把生成图片的程序专门做一个页面如:
picture.aspx :<img src="picture.aspx">
你如果是带参数生成的话,那么:
<img src="picture.aspx?id=424">思路就是这样
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。