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"/>