/// <summary> ///Create a verification code/// </summary> /// <returns></returns> Private stringGeneratecheckcode () {intNumberCharCode; stringCheckcode =String.Empty; System.Random Random=NewRandom (); for(inti =0; I <4; i++) { number=Random. Next (); if(Number%2==0) {Code= (Char)('0'+ (Char) (number%Ten)); } Else{Code= (Char)('A'+ (Char) (number% -)); } Checkcode+=code. ToString (); } //Response.Cookies.Add (New HttpCookie ("Checkcode", Checkcode)); returnCheckcode; } /// <summary> ///Create a validation picture/// </summary> /// <param name= "Checkcode" ></param> Private voidCreatecheckcodeimage (stringCheckcode) { if(Checkcode = =NULL|| Checkcode.trim () = =String.Empty)return; System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap ((int) math.ceiling ((Checkcode.length *12.5)), 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 for(inti =0; I <2; 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.black), x1, y1, x2, y2); } Font Font=NewSystem.Drawing.Font ("Arial", A, (System.Drawing.FontStyle.Bold |System.Drawing.FontStyle.Italic)); 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); //draw the foreground noise point of the picture for(inti =0; I < -; i++) { intx =Random. Next (image. Width); inty =Random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); } //draw the border line of a 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.GIF); Response.clearcontent (); Response.ContentType="Image/gif"; Response.BinaryWrite (Ms. ToArray ()); } finally{g.dispose (); Image. Dispose (); } }