Generate a random Verification Code

Source: Internet
Author: User

Protected void page_load (Object sender, eventargs E)
{
// Generate the verification code
String checkcode = randletter (4 );
// Save the new verification code to the session
Session ["checkcode"] = checkcode;
// Enter the verification code
Createimages (checkcode );
}

/// <Summary>
/// Generate verification Image
/// </Summary>
/// <Param name = "checkcode"> Verification character </param>
Private void createimages (string checkcode)
{
Int iwidth = (INT) (checkcode. length * 13 );
System. Drawing. bitmap image = new system. Drawing. Bitmap (iwidth, 23 );
Graphics G = graphics. fromimage (image );
G. Clear (color. White );
// Define the color
Color [] C = {color. Black, color. Red, color. darkblue, color. Green, color. Orange, color. Brown, color. darkcyan, color. Purple };
// Define the font
String [] font = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""};
Random Rand = new random ();
// Random output noise
For (INT I = 0; I <50; I ++)
{
Int x = Rand. Next (image. width );
Int y = Rand. Next (image. Height );
G. drawrectangle (new pen (color. lightgray, 0), X, Y, 1, 1 );
}

// Output verification code characters of different fonts and colors
For (INT I = 0; I <checkcode. length; I ++)
{
Int cIndex = Rand. Next (7 );
Int findex = Rand. Next (5 );

Font F = new system. Drawing. Font (font [findex], 10, system. Drawing. fontstyle. Bold );
Brush B = new system. Drawing. solidbrush (C [cIndex]);
Int II = 4;
If (I + 1) % 2 = 0)
{
II = 2;
}
G. drawstring (checkcode. substring (I, 1), F, B, 3 + (I * 12), II );
}
// Draw a border
G. drawrectangle (new pen (color. Black, 0), 0, 0, image. Width-1, image. Height-1 );

// Output to the browser
System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Httpcontext. Current. response. clearcontent ();
// Response. clearcontent ();
Httpcontext. Current. response. contenttype = "image/JPEG ";
Httpcontext. Current. response. binarywrite (Ms. toarray ());
G. Dispose ();
Image. Dispose ();
}

/// <Summary>
/// Generate random letters
/// </Summary>
/// <Param name = "count"> Number of Generated letters </param>
/// <Returns> generated Verification Code </returns>
Private string randletter (INT count)
{
String allletters = ", 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 [] allchar = allletters. Split (',');
String checkcode = "";
Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.

// Use a simple algorithm to ensure different random numbers are generated
Random Rand = new random ();
For (INT I = 1; I <count + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (allchar. Length );
Temp = T;
Checkcode + = allchar [T];
}
Return checkcode;
}

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.