. NET common tricks _c# create a verification code

Source: Internet
Author: User

/// <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 (); }    } 

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.