ASP. NET Simple Verification Code verification Implementation Code

Source: Internet
Author: User
The first is to create a new verification code page validatecode.aspx
Defining variables so that they are useful for later modification

private int codelen = 4;//captcha length private int fineness = 85;//picture sharpness private int imgwidth = 48;//picture width private int imgheight = 24;//picture Height private string fontFamily = "Times New Roman";//font name private int fontSize = 14;//font size 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 V Alidatecode = ""; Random random = new random (), or//stochastic object for (int i = 0; i < Codelen; i++)//loop generates each bit value {int n = random. Next (10);//digital Validatecode + = n.tostring ();  } session["Vcode"] = validatecode;//Save Verification Code return validatecode;//back verification code} private void Disturbbitmap (Bitmap Bitmap)//image Background { Random random = new random ();//generate for (int i = 0; i < bitmap) by randomly number. Width; i++)//Generate {for (int j = 0; J < bitmap) per pixel by loop nesting. Height; J + +) {if (random. Next (<= this.fineness) bitmap. SetPixel (I,j,color.lightgray); }}} private void Drewvalidatecode (Bitmap bitmap,string validatecode)//Draw Verification code image {Graphics g = graphics.fromimage (Bitmap); //Gets the drawing object font font = new Font (fontfamily,fontsize,fontstyle.bold);//sets the drawing font g.drawstring (Validatecode,font, Brushes.black,posx,posy);//Draw Captcha Image}

The last is to call the

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 Verification code image bitmap. Save (response.outputstream,imageformat.gif);//preserve image, wait for output}

Validatecode.aspx Page Completed
The rest is simple. Create a new page

<asp:image id= "Image1" runat= "Server" height= "21px" width= "61px" imageurl= "~/default2.aspx" imagealign= "Middle"/ >

Post-run effects

When committing, the value in the text box is compared with session["vcode"] = validatecode;//to save the verification code to determine if the input is correct.

Related Article

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.