问题描述
请在.net里怎么写验证码我想在登入页面加上验证码怎么写给个源码吧我的20分谢谢帮解答下吧
解决方案
解决方案二:
额~~~代码我有,但我也不是完全理解,在上面我写了简单的注释,只能简单的讲解!!publicpartialclassCode:System.Web.UI.Page{string_str_code="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";int_int_length=4;protectedvoidPage_Load(objectsender,EventArgse){stringstr_code=code_view();Session["code"]=str_code;view_img(str_code);}///<summary>///生成验证码的随机数///</summary>///<returns></returns>privatestringcode_view(){stringreturn_str="";string[]str_arr=_str_code.Split(newchar[]{','});Randomr=newRandom(unchecked((int)DateTime.Now.Ticks));//让产生的随机数不重复//产生随机数intint_start=-1;for(inti=0;i<_int_length;i++){int_start=r.Next(0,str_arr.Length-1);return_str+=str_arr[int_start];}returnreturn_str;}///<summary>///用GDI+将随机数绘制到背景图片上///</summary>///<paramname="str_code"></param>privatevoidview_img(stringstr_code){Bitmapimg1=newBitmap(70,30);//用位图定位图片的大小//绘制验证码Graphicsps=Graphics.FromImage(img1);ps.Clear(Color.White);Fontft=newFont("Arial",14,FontStyle.Bold);LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(0,0,img1.Width,img1.Height),Color.Blue,Color.DarkOrange,1.2f);System.Drawing.Imageimg2=System.Drawing.Image.FromFile(Server.MapPath("~")+@"BackImage.gif");ps.DrawImage(img2,newRectangle(0,0,img1.Width,img1.Height));ps.DrawString(str_code,ft,brush,2,2);ps.DrawRectangle(newPen(Color.Silver),newRectangle(0,0,img1.Width-1,img1.Height-1));//将编辑好的验证码保存到内存中MemoryStreamms=newMemoryStream();img1.Save(ms,ImageFormat.Gif);//将内存中的验证码输出到页面Response.ClearContent();Response.ContentType="image/Gif";Response.BinaryWrite(ms.ToArray());ms.Dispose();ps.Dispose();}}
解决方案三:
网上有验证控件可以下载的。用起来很简单又很美观。
解决方案四:
冒泡google
解决方案五:
xiexie我在研究下
解决方案六:
我写过一个简单的,代码忘存哪了。就是自定义个空件,我是继承的panel,然后随机生成你想要随机码,然后画到panel里,如果想有点背景,你再处理下。
解决方案七:
请参考一下:http://www.cnblogs.com/liudao/archive/2007/04/29/732065.html
解决方案八:
在google中搜..很多很多
解决方案九:
网上有很多直接找我给帖一段参考下usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;publicpartialclassValidateCode:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){BitmapnewBitmap=newBitmap(75,27,System.Drawing.Imaging.PixelFormat.Format32bppArgb);Graphicsg=Graphics.FromImage(newBitmap);Randomr=newRandom();g.Clear(Color.White);Colorc1=Color.FromArgb(64,184,6);System.Drawing.Drawing2D.LinearGradientBrushbrush=newSystem.Drawing.Drawing2D.LinearGradientBrush(newRectangle(0,10,75,27),c1,c1,0.0f,true);//for(inti=0;i<25;i++)//{//intx1=r.Next(newBitmap.Width);//intx2=r.Next(newBitmap.Width);//inty1=r.Next(newBitmap.Height);//inty2=r.Next(newBitmap.Height);//g.DrawLine(newPen(Color.Silver),x1,y1,x2,y2);//}//for(inti=0;i<100;i++)//{//intx=r.Next(newBitmap.Width);//inty=r.Next(newBitmap.Height);//newBitmap.SetPixel(x,y,Color.FromArgb(r.Next()));//}Colorc=Color.FromArgb(234,234,234);g.FillRectangle(newSolidBrush(c),0,0,75,27);stringvalue=GenerateRandom(4);Fontfont=newFont("Arial",18,System.Drawing.FontStyle.Regular);g.DrawString(value,font,brush,0,0);g.DrawRectangle(newPen(Color.White),0,0,75,27);//g.FillRectangle(newSolidBrush(Color.Gray),0,12,80,1);System.IO.MemoryStreamms=newSystem.IO.MemoryStream();newBitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);Response.ClearContent();Response.ContentType="image/gif";Response.BinaryWrite(ms.ToArray());Session["validatecode"]=value;}privatestaticchar[]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','Z','X','Y','Z',};publicstaticstringGenerateRandom(intLength){System.Text.StringBuildernewRandom=newSystem.Text.StringBuilder(36);Randomrd=newRandom();for(inti=0;i<Length;i++){newRandom.Append(constant[rd.Next(36)]);}returnnewRandom.ToString();}}