usingSystem;usingSystem.Collections;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Xml.Linq;usingSystem.Drawing;usingSystem.Web.SessionState;namespacemt{/// <summary> ///Summary description of $codebehindclassname $/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] Public classVercode:ihttphandler, IRequiresSessionState {PrivateRandom Randomseed =NewRandom (); Public voidProcessRequest (HttpContext context) {//generate random number iIlLoO0 stringStrword ="23456789QWERTYUPASDFGHKXCVBNM"; stringNumstr =NULL; for(inti =0; I <5; i++) {Numstr+ = Strword[randomseed.next (0, Strword.length)]; } //Save the Verification code to the sessionContext. session["Vcode"] =Numstr.tolower (); Createimages (context, NUMSTR); } #regionGenerate a Captcha picturePrivate voidCreateimages (HttpContext context,stringCheckcode) { intIwidth = (int) (Checkcode.length * -); System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap (iwidth, A); Graphics g=graphics.fromimage (image); G.clear (Color.White); //Define ColorColor[] C ={color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color.Purple}; //Defining Fonts string[] Font = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","Song Body" }; Random Rand=NewRandom (); //random output noise. for(inti =0; I < -; i++) { intx =Rand. Next (image. Width); inty =Rand. Next (image. Height); G.drawrectangle (NewPen (Color.lightgray,0), X, Y,1,1); } //output CAPTCHA characters for different fonts and colors for(inti =0; i < checkcode.length; i++) { intCIndex = Rand. Next (7); intFindex = Rand. Next (5); Font F=NewSystem.Drawing.Font (Font[findex],Ten, System.Drawing.FontStyle.Bold); Brush b=NewSystem.Drawing.SolidBrush (C[cindex]); intII =4; if((i +1) %2==0) {II=2; } g.drawstring (Checkcode.substring (i,1), F, B,2+ (I * A), ii); } //Draw a borderG.drawrectangle (NewPen (colortranslator.fromhtml ("#CCCCCC"),0),0,0, image. Width-1, image. Height-1); //Output to BrowserSystem.IO.MemoryStream ms =NewSystem.IO.MemoryStream (); Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); Context. Response.clearcontent (); //response.clearcontent ();Context. Response.ContentType ="Image/gif"; Context. Response.BinaryWrite (Ms. ToArray ()); G.dispose (); Image. Dispose (); } #endregion Public BOOLisreusable {Get { return false; } } }}
C # Verification Code