Background:
/// <summary> ///Create a picture of the verification code/// </summary> /// <param name= "Validatecode" >Verification Code</param> PublicActionResult Createvalidategraphic (stringValidatecode) {Bitmap image=NewBitmap ((int) math.ceiling (Validatecode.length *14.0), the); Graphics g=graphics.fromimage (image); Try { //Generate random GeneratorsRandom random =NewRandom (); //clear the background color of the pictureg.clear (Color.White); //interference lines for drawing pictures for(inti =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 = new Font ("Arial", page, (FontStyle.Bold | Fontstyle.italic)); string[] FontName = {"XXFarEastFont-Chinese new Wei","Song Body","Round Body","blackbody","Official Script" }; Font Font=NewFont (fontname[NewRandom (). Next (0, Validatecode.length)], A, (FontStyle.Bold |fontstyle.italic)); LinearGradientBrush Brush=NewLinearGradientBrush (NewRectangle (0,0, image. Width, image. Height), Color.Blue, color.darkred,1.2f,true); g.DrawString (Validatecode, font, brush,3,8); //foreground interference point of picture painting 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); //Save picture DataMemoryStream stream =NewMemoryStream (); Image. Save (stream, imageformat.jpeg); //Output Picture Stream//return stream. ToArray (); returnFile (stream. ToArray (),@"Image/jpeg"); } finally{g.dispose (); Image. Dispose (); } }
Foreground call:
The parameter represents the content of the CAPTCHA, which can be replaced by the previous or random number of GUIDs
MVC Verification Code