Implementation of verification code
".. /common/webvcode.aspx"title="can't see? Click here to replace"alt="can't see? Click here to replace"onclick="this.src= '/common/webvcode.aspx?a= ' +math.random ();"/> protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) {session["Vcode"] = CreateImage (5, Verificationtype.numeric); } } /// <summary> ///type of verification code/// </summary> Public enumVerificationtype {/// <summary> ///only Numbers/// </summary>Numeric,/// <summary> ///numbers and English characters/// </summary>Numericandchar,/// <summary> ///Chinese characters/// </summary>Chinesechar}/// <summary> ///generate a random text image, saved in session["Code1"]/// </summary> /// <param name= "Count" >the number of words in the picture</param> /// <returns>the generated text</returns> Public stringCreateImage (intcount, Verificationtype type) { stringValidcode =Gencode (count, type); Switch(type) { CaseVerificationType.Numeric:CreateCheckCodeImage (Validcode,13.5); Break; CaseVerificationType.NumericAndChar:CreateCheckCodeImage (Validcode, -); Break; CaseVerificationType.ChineseChar:CreateCheckCodeImage (Validcode,22.5); Break; default: Break; } returnValidcode; } /// <summary> ///Generate random string/// </summary> /// <param name= "num" >randomly out a few characters</param> /// <returns>randomly-out string</returns> Private stringGencode (intnum, Verificationtype type) { stringstr =string. Empty; Switch(type) { CaseVerificationType.Numeric:str="0123456789"; Break; CaseVerificationType.NumericAndChar:str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Break; CaseVerificationtype.chinesechar: Break; default: Str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Break; } Char[] Chastr =Str. ToCharArray (); stringCode =""; Random Rd=NewRandom (); inti; for(i =0; i < num; i++) {Code+ = str. Substring (Rd. Next (0Str. Length),1); } returnCode; } /// <summary> ///Create a picture (add background noise, foreground noise points)/// </summary> /// <param name= "Checkcode" >Random out string</param> Private voidCreatecheckcodeimage (stringCheckcode,Doublecodewidth) { if(Checkcode.trim () = =""|| Checkcode = =NULL) return; System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap ((int) (Checkcode.length * codewidth), A); Graphics g=graphics.fromimage (image); Try { //Generate random GeneratorsRandom random =NewRandom (); //clear the background color of the pictureg.clear (Color.White); //draw a picture of the background noise line inti; for(i =0; I < -; i++) { intX1 =Random. Next (image. Width); intx2 =Random. Next (image. Width); intY1 =Random. Next (image. Height); inty2 =Random. Next (image. Height); G.drawline (NewPen (color.silver), x1, y1, x2, y2); } Font Font=NewSystem.Drawing.Font ("Verdana", A, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush Brush=NewSystem.Drawing.Drawing2D.LinearGradientBrush (NewRectangle (0,0, image. Width, image. Height), Color.Blue, color.darkred,1.2F,true); g.DrawString (Checkcode, font, brush,2,2,NewSystem.Drawing.StringFormat ()); //draw the foreground noise point of the pictureG.drawrectangle (NewPen (Color.silver),0,0, image. Width-1, image. Height-1); System.IO.MemoryStream Ms=NewSystem.IO.MemoryStream (); Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); HttpContext.Current.Response.ExpiresAbsolute= System.DateTime.Now.AddMilliseconds (0); //clear the page output cache, set the page without cachingResponse.Buffer =true; Response.ExpiresAbsolute= System.DateTime.Now.AddMilliseconds (0); Response.Expires=0; Response.CacheControl="No-cache"; Response.appendheader ("Pragma","No-cache"); HttpContext.Current.Response.ClearContent (); HttpContext.Current.Response.ContentType="Image/jpeg"; HttpContext.Current.Response.BinaryWrite (Ms. ToArray ()); G.dispose (); Image. Dispose (); } Catch{g.dispose (); Image. Dispose (); } }