How does Asp.net generate image verification codes (simple)

Source: Internet
Author: User
Create a page image. aspx and add a namespace:

Using system. Drawing. imaging;
Using system. IO;

Then, in the page_load Event Callback, enter the followingCode:

// Generate a four-digit Verification Code
String TMP = rndnum (4 );
Httpcookie A = new httpcookie ("imagev", TMP );
Response. Cookies. Add ();
This. validatecode (TMP );

Next, add two methods to the page:

Private void validatecode (string vnum)
{
Bitmap IMG = NULL;
Graphics G = NULL;
Memorystream MS = NULL;

int gheight = vnum. length * 12;
IMG = new Bitmap (gheight, 25);
G = graphics. fromimage (IMG);
// background color
G. clear (color. white);
// text font
font F = new font ("Arial black", 10 );
// text color
solidbrush S = new solidbrush (color. black);
G. drawstring (vnum, F, S, 3, 3);
MS = new memorystream ();
IMG. save (MS, imageformat. JPEG);
response. clearcontent ();
response. contenttype = "image/JPEG";
response. binarywrite (Ms. toarray ();
G. dispose ();
IMG. dispose ();
response. end ();
}

private string rndnum (INT vcodenum)
{< br> string vchar = ", a, B, c, d, e, F, G, H, I, J, K, L, M, N, O, P "+
", Q, R, S, T, U, v, W, X, Y, Z ";
string [] vcarray = vchar. split (New char [] {','});
string vnum = "";
int temp =-1;

Random Rand = new random ();

For (INT I = 1; I <vcodenum + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}

Int T = Rand. Next (35 );
If (temp! =-1 & temp = T)
{
Return rndnum (vcodenum );
}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum;
}

If you want to generate a verification code in A. aspx on the page, add an image control on the page, assuming the name is imagebutton1, and then write the following code in the page_load event:

Imagebutton1.imageurl = "image. aspx ";

This allows you to generate a verification code, image. the ASPX page can be stored anywhere, but you must note that imagebutton1.imageurl must be written to the same level. You can directly write the image at the same level. aspx, upper-level write .. /image. aspx, very convenient.

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.