Generate random verification code of any number of digits

Source: Internet
Author: User

 

1. First write a method to generate a random number of 0-9, A-Z 35 characters without repeated digits, and the digits are passed as parameters.

 

  ///   <Summary>
/// Generate a random number of specified digits
///   </Summary>
///   <Param name = "vcodenum"> Number of digits generated </Param>
///   <Returns> </returns>
Private   String Rndnum ( Int Vcodenum)
{
String Vchar =   " " ;
Vchar = 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 " ;
Vchar = Vchar. tolower ();
String [] Vcarray = Vchar. Split ( ' , ' );
String Vnum =   "" ; // Stringbuilder is not needed because the string is short.
Int Temp =   - 1 ; // Record the last random value and try to avoid producing several identical random numbers.

// Use a simpleAlgorithmTo ensure different random numbers are generated
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];

}
ReturnVnum;
}

 

2. Define a method to draw the verification code.

 

  ///   <Summary>
/// Create a verification code for the specified array
///   </Summary>
///   <Param name = "checkcode"> Verification Code </Param>
Private   Void Createimage ( String Checkcode)
{
Int Iwidth = ( Int ) (Checkcode. Length *   15 );
System. Drawing. bitmap image =   New System. Drawing. Bitmap (iwidth, 25 );
Graphics g = Graphics. fromimage (image );
G. Clear (color. White );
// Define color
Color [] C = {Color. Black, color. Red, color. darkblue, color. Green, color. Brown, color. darkcyan, color. Purple, color. yellowgreen };
// Define font
String [] Font = { " Times New Roman " , " 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 );
G. drawline ( New Pen (color. lightpink, 0 ), X, y, 50 , 23 );
}

// Outputs Verification Code characters of different fonts and colors
For ( Int I =   0 ; I < Checkcode. length; I ++ )
{
Int CIndex = Rand. Next ( 7 ); // Random from 7 colors
Int Findex = Rand. Next ( 6 ); // Randomly retrieve from 6 Fonts

Font F =   New System. Drawing. Font (font [findex], 13 , System. Drawing. fontstyle. Regular );
Brush B =   New System. Drawing. solidbrush (C [cIndex]);
Int II =   4 ;
// The width of the characters generated for an even number is 4, and the value of the odd number is 2.
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 Browser
MS for system. Io. memorystream =   New System. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Response. clearcontent ();
Response. contenttype =   " Image/JPEG " ;
Response. binarywrite (Ms. toarray ());
G. Dispose ();
Image. Dispose ();
}

3. Test in the page_load event

 

  Protected   Void Page_load ( Object Sender, eventargs E)
{

If ( ! Page. ispostback)
{
String Code =   This . Rndnum ( 4 );
Session [ " Code " ] = Code;
This . Createimage (CODE );
}
}

 

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.