Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. Drawing. design;
Using System. drawing;
Public partial class checkcode: system. web. UI. page
{< br> protected void page_load ( Object sender, eventargs e)
{< br> This . createcheckcodeimage (rndnum ();
}
private string rndnum ()
{< br> int number;
char code;
string checkcode = string. empty;
System. Random random= NewRandom ();
For ( Int I = 0 ; I < 4 ; I ++ )
{
Number = Random. Next ();
Code = ( Char )( ' 0 ' + ( Char ) (Number % 10 )); // Number
// If (Number % 2 = 0)
// Code = (char) ('0' + (char) (Number % 10 )); // Number
// Else
// Code = (char) ('A' + (char) (Number % 26 )); // Letter
Checkcode + = Code. tostring ();
}
Response. Cookies. Add ( New Httpcookie ( " Checkcode " , Checkcode ));
Return Checkcode;
}
Private Void Createcheckcodeimage ( String Checkcode)
{
If (Checkcode = Null | Checkcode. Trim () = String. Empty)
Return ;
System. Drawing. bitmap image = New System. Drawing. Bitmap (( Int ) Math. Ceiling (checkcode. Length * 12.5 )), 22 );
Graphics g = Graphics. fromimage (image );
Try
{
// Generate random Generator
Random random = New Random ();
// Clear background color
G. Clear (color. White );
// Background noise line
For ( Int I = 0 ; I < 25 ; I ++ )
{
// Int X1 = random. Next (image. width );
// Int X2 = random. Next (image. width );
// Int Y1 = random. Next (image. Height );
// Int y2 = random. Next (image. Height );
// G. drawline (new pen (color. Silver), X1, Y1, X2, Y2 );
}
Font font = New System. Drawing. Font ( " Arial " , 12 , (System. Drawing. fontstyle. Regular | System. Drawing. fontstyle. italic ));
System. Drawing. drawing2d. lineargradientbrush = New System. Drawing. drawing2d. lineargradientbrush ( New Rectangle ( 0 , 0 , Image. Width, image. Height), color. Blue, color. darkred, 1.2f , True );
G. drawstring (checkcode, Font, brush, 2 , 2 );
// Foreground Noise
For ( Int I = 0 ; I < 100 ; I ++ )
{
// Int x = random. Next (image. width );
// Int y = random. Next (image. Height );
// Image. setpixel (X, Y, color. fromargb (random. Next ()));
}
// Draw the border line of the image
G. drawrectangle ( New Pen (color. Silver ), 0 , 0 , Image. Width - 1 , Image. Height - 1 );
MS for system. Io. memorystream = New System. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = " Image/GIF " ;
Response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
}