A simple program for generating logon verification Codes

Source: Internet
Author: User
Function. CS /**/ ///   <Summary>
/// Generate random number
///   </Summary>
///   <Param name = "codecount"> Number of random numbers to be generated </Param>
///   <Returns> </returns>
Public   Static   String Createrandomcode ( Int Codecount)
{
String Allchar =   " " ;
String [] Allchararray = Allchar. Split ( ' , ' );
String Randomcode =   "" ;
Int Temp =   - 1 ;

Random Rand =   New Random ();
For ( Int I =   0 ; I < Codecount; I ++ )
{
If (Temp ! =   - 1 )
{
Rand= NewRandom (I*Temp*((Int) Datetime. Now. ticks ));
}
Int T = Rand. Next ( 9 );
If (Temp = T)
{
ReturnCreaterandomcode (codecount );
}
Temp = T;
Randomcode + = Allchararray [T];
}
Return Randomcode;
}
/**/ /// <Summary>
///Generate Verification Code Image
/// </Summary>
/// <Param name = "checkcode"> </param>
Public   Static   Void Createimage ( String Checkcode)
{
Int Iwidth = ( Int ) (Checkcode. Length *   11.5 );
System. Drawing. bitmap image =   New System. Drawing. Bitmap (iwidth, 20 );
Graphics g = Graphics. fromimage (image );
Font F =   New System. Drawing. Font ( " Arial " , 10 , System. Drawing. fontstyle. Bold );
Brush B =   New System. Drawing. solidbrush (color. White );
// G. fillrectangle (new system. Drawing. solidbrush (color. Blue), 0, 0, image. Width, image. Height );
G. Clear (color. Blue );
G. drawstring (checkcode, F, B, 3 , 3 );

Pen blackpen =   New Pen (color. Brown, 0 );
Random Rand =   New Random ();
For ( Int I = 0 ; I < 5 ; I ++ )
{
IntY=Rand. Next (image. Height );

//G. drawline (blackpen, 0, Y, image. Width, y );
}

MS for system. Io. memorystream =   New System. Io. memorystream ();
For ( Int I = 0 ; I < 90 ; I ++ )
{< br> int x = Rand. next (image. width);
int Y = Rand. next (image. height);
image. setpixel (X, Y, color. fromargb (Rand. next ();
}
Image. Save (MS, system. Drawing. imaging. imageformat. JPEG );
System. Web. httpcontext. Current. response. clearcontent ();
System. Web. httpcontext. Current. response. contenttype =   " Image/JPEG " ;
System. Web. httpcontext. Current. response. binarywrite (Ms. toarray ());
G. Dispose ();
Image. Dispose ();
}

Validatecode. aspx. CS Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Place user code here to initialize the page
String Checkcode = Lemongtree. BLL. function. createrandomcode ( 4 );
Session [ " Checkcode " ] = Checkcode;
Lemongtree. BLL. function. createimage (checkcode );
}

. Aspx file call
<Asp: Image id = "image1" runat = "server" imageurl = "validatecode. aspx"/>

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.