操作系统

当前位置:澳门新莆京娱乐官网 > 操作系统 > NET实现动态GIF验证码

NET实现动态GIF验证码

来源:http://www.drgigabytes.com 作者:澳门新莆京娱乐官网 时间:2020-02-07 12:02

澳门新莆京娱乐官网 1

背景:12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样。而多家抢票软件接近“报废”,引发不少网友不满的吐槽称“太抽象太艺术了”。正题:以前做项目有时候也会用到验证码,但基本都是静态的。这次也想凑凑12306的热闹。闲言少续,切入正题,先上代码,实现方法:

publicvoidShowCode(){//对象实例化ValidateGifValidate=newValidate();#region对验证码进行设置(不进行设置时,将以默认值生成)//验证码位数,不小于4位GifValidate.ValidateCodeCount=4;//验证码字体型号(默认13)GifValidate.ValidateCodeSize=13;//验证码图片高度,高度越大,字符的上下偏移量就越明显GifValidate.ImageHeight=23;//验证码字符及线条颜色(需要参考颜色类)GifValidate.DrawColor=System.Drawing.Color.BlueViolet;//验证码字体(需要填写服务器安装的字体)GifValidate.ValidateCodeFont="Arial";//验证码字符是否消除锯齿GifValidate.FontTextRenderingHint=false;//定义验证码中所有的字符(","分离),似乎暂时不支持中文GifValidate.AllChar="1,2,3,4,5,6,7,8,9,0,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";#endregion//输出图像(Session名称)GifValidate.OutPutValidate("GetCode");}

澳门新莆京娱乐官网,调用主要方法:

publicclassValidate{publicstringAllChar="1,2,3,4,5,6,7,8,9,0,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";publicColorDrawColor=Color.BlueViolet;publicboolFontTextRenderingHint=false;publicintImageHeight=0x17;privatebyteTrueValidateCodeCount=4;protectedstringValidateCode="";publicstringValidateCodeFont="Arial";publicfloatValidateCodeSize=13f;privatevoidCreateImageBmp(outBitmapImageFrame){char[]chArray=this.ValidateCode.ToCharArray(0,this.ValidateCodeCount);intwidth=(int)(((this.TrueValidateCodeCount*this.ValidateCodeSize)*1.3)+4.0);ImageFrame=newBitmap(width,this.ImageHeight);Graphicsgraphics=Graphics.FromImage(ImageFrame);graphics.Clear(Color.White);Fontfont=newFont(this.ValidateCodeFont,this.ValidateCodeSize,FontStyle.Bold);Brushbrush=newSolidBrush(this.DrawColor);intmaxValue=(int)Math.Max((float)((this.ImageHeight-this.ValidateCodeSize)-3f),(float)2f);Randomrandom=newRandom();for(inti=0;ithis.TrueValidateCodeCount;i++){int[]numArray=newint[]{(((int)(i*this.ValidateCodeSize))+random.Next(1))+3,random.Next(maxValue)};Pointpoint=newPoint(numArray[0],numArray[1]);if(this.FontTextRenderingHint){graphics.TextRenderingHint=TextRenderingHint.SingleBitPerPixel;}else{graphics.TextRenderingHint=TextRenderingHint.AntiAlias;}graphics.DrawString(chArray[i].ToString(),font,brush,(PointF)point);}graphics.Dispose();}privatevoidCreateImageGif(){AnimatedGifEncoderencoder=newAnimatedGifEncoder();MemoryStreamstream=newMemoryStream();encoder.Start();encoder.SetDelay(5);encoder.SetRepeat(0);for(inti=0;i10;i++){Bitmapbitmap;this.CreateImageBmp(outbitmap);this.DisposeImageBmp(refbitmap);bitmap.Save(stream,ImageFormat.Png);encoder.AddFrame(Image.FromStream(stream));stream=newMemoryStream();}encoder.OutPut(refstream);HttpContext.Current.Response.ClearContent();HttpContext.Current.Response.ContentType="image/Gif";HttpContext.Current.Response.BinaryWrite(stream.ToArray());stream.Close();stream.Dispose();}privatevoidCreateValidate(){this.ValidateCode="";string[]strArray=this.AllChar.Split(newchar[]{','});intindex=-1;Randomrandom=newRandom();for(inti=0;ithis.ValidateCodeCount;i++){if(index!=-1){random=newRandom((i*index)*((int)DateTime.Now.Ticks));}intnum3=random.Next(0x23);if(index==num3){this.CreateValidate();}index=num3;this.ValidateCode=this.ValidateCode+strArray[index];}if(this.ValidateCode.Lengththis.TrueValidateCodeCount){this.ValidateCode=this.ValidateCode.Remove(this.TrueValidateCodeCount);}}privatevoidDisposeImageBmp(refBitmapImageFrame){Graphicsgraphics=Graphics.FromImage(ImageFrame);Penpen=newPen(this.DrawColor,1f);Randomrandom=newRandom();Point[]pointArray=newPoint[2];for(inti=0;i15;i++){pointArray[0]=newPoint(random.Next(ImageFrame.Width),random.Next(ImageFrame.Height));pointArray[1]=newPoint(random.Next(ImageFrame.Width),random.Next(ImageFrame.Height));graphics.DrawLine(pen,pointArray[0],pointArray[1]);}graphics.Dispose();}publicvoidOutPutValidate(stringValidateCodeSession){this.CreateValidate();this.CreateImageGif();HttpContext.Current.Session[ValidateCodeSession]=this.ValidateCode;}publicbyteValidateCodeCount{get{returnthis.TrueValidateCodeCount;}set{if(value4){this.TrueValidateCodeCount=value;}}}}

验证码效果:下载

本文由澳门新莆京娱乐官网发布于操作系统,转载请注明出处:NET实现动态GIF验证码

关键词: