Write login Verification code in C # (asp.net)

Source: Internet
Author: User

public class ValidateCode:System.Web.UI.Page


{





private void Page_Load (object sender, System.EventArgs e)


  {





this. Createcheckcodeimage (Generatecheckcode ());


  }


#region The code generated by the Web Forms Designer override


protected void OnInit (EventArgs e)


  {


    //


//CodeGen: This call is required for the ASP.net Web forms Designer.


    //





InitializeComponent ();


base. OnInit (e);


  }





///<summary>


///Designer supports the desired method-do not modify
with the Code Editor

///The contents of this method.


///</summary>


private void


InitializeComponent ()


  {





this. Load + = new System.EventHandler (this. Page_Load);


  }


#endregion


Private


string Generatecheckcode ()


  {





int number;





char code;





string checkcode = String.Empty;


system.random Random = new Random ();


for (int i=0;i<5;i++)


    {


number = random. Next ();


if (number% 2 = 0)


code = (char) (' 0 ' + (char) (number% 10));


Else


code = (char) (' A ' + (char) (number% 26));


Checkcode + = code. ToString ();


    }


Response.Cookies.Add (New HttpCookie ("Checkcode", Checkcode));


return checkcode;


  }





private void Createcheckcodeimage (string checkcode)


  {


if (Checkcode = null | | Checkcode.trim () = = String.Empty)


return;


System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22 );


Graphics g = graphics.fromimage (image);


Try


    {


//generating random generators


Random Random = new Random ();


//Empty picture background color


g.clear (Color.White);


//Picture background noise line


for (int i=0;i<25;i++)





      {





int x1 = random. Next (image. Width);





int x2 = random. Next (image. Width);





int y1 = random. Next (image. Height);





int y2 = random. Next (image. Height);


G.drawline (New Pen (color.silver), x1, y1, x2, y2);


}


Font font = new System.Drawing.Font (Arial), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));


System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rec Tangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2f, true);


g.drawstring (checkcode, Font, brush, 2, 2);


//Picture of the foreground noise point


for (int i=0;i<100;i++)


      {





int x = random. Next (image. Width);





int y = random. Next (image. Height);


image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));


      }


//Picture border line


G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);


System.IO.MemoryStream ms = new System.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.