MVC Verification Code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.