Asp. The generation of login verification code and verification of input verification code in net

Source: Internet
Author: User

One: code Generation Implementation codes

protected void Page_Load (object sender, EventArgs E)
{
String Validatecode = Createvalidatecode ();//generate Verification Code
Bitmap Bitmap = new Bitmap (imgwidth, imgheight);//generate Bitmap image
Disturbbitmap (bitmap); Image background
Drewvalidatecode (bitmap, Validatecode);//draw captcha image
Bitmap. Save (response.outputstream, imageformat.gif);//preserve image, wait for output

}

    private int codelen = 4;//captcha length
    private int fineness = 85;//picture sharpness
 & nbsp;  private int imgwidth = 48;//picture width
    private int imgheight = 24;//picture height
  &nbs P private string fontFamily = "times New Roman";//font name
    private int fontSize = 14;//font size
 &nb sp; //private int fontstyle = 0;//font style
    private int posX = 0;//draw starting coordinates x
    private int posY = 0;//draw coordinates y

    private String Createvalidatecode ()//generate verification Code
    {
         string validatecode = "";
        Random random = new random ();//random Number Object
         for (int i = 0; i < codelen; i++)//loop generates Per-digit value
        {
&nb sp;           int n = random. Next (10);//digital
            validatecode + = n.tostring ();
       }
        session["vcode"] = Validatecode;//save verification Code This session is called at the Foreground.
        return validatecode;//returns verification code
   }

    private void Disturbbitmap (Bitmap Bitmap)//image background
    {
         random random = new random ();//generate
        for ( int i = 0; I < bitmap. Width; I++)//generate
        {
       from pixel by bit by looping through nesting       for (int j = 0; J < bitmap. Height; J + +)
            {
                 if (random. Next (<= This.fineness)
                     bitmap. SetPixel (i, j, color.lightgray);
           }
       }
   }

private void Drewvalidatecode (Bitmap Bitmap, string Validatecode)//draw authenticode image
{
Graphics g = Graphics.fromimage (bitmap);//get the drawing object
Font font = new Font (fontFamily, fontSize, Fontstyle.bold);//set drawing font
g.DrawString (validatecode, font, brushes.black, posX, posY);//draw captcha image
}

Second: Verify the correctness of the verification code when landing

If (TextBox1.Text.ToUpper (). Equals (session["vcode"]+ "")) "session[" vcode "] is the Session value saved when generating the captcha"

Asp. The generation of login verification code and verification of input verification code in net

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.