Generate graphic Verification Code Public Class Ajax_safecode: pageextend
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Httpcontext. Current. session [ " Checkcode " ] = Rndnum ( 4 );
Createimages (httpcontext. Current. session [ " Checkcode " ]. Tostring ());
}
/// <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, 20 );
Graphics g = Graphics. fromimage (image );
G. Clear (color. White );
// Define color
Color [] C = {Color. Black, color. Red, color. darkblue, color. Green, color. Orange, color. Brown, color. darkcyan, color. Purple };
// Define 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 );
}
// Outputs 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 Browser
MS for system. Io. memorystream = 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 = "vcodenum"> Number of Generated letters </Param>
/// <Returns> String </Returns>
Private String Rndnum ( Int Vcodenum)
{
String Vchar = " 0, 1, 2, 3, 4, 6, 7, 8, 9, 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 ( ' , ' );
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 (vcarray. Length );
If (Temp ! = - 1 && Temp = T)
{
Return Rndnum (vcodenum );
}
Temp = T;
Vnum + = Vcarray [T];
}
ReturnVnum;
}
}