Private string generatecheckcode () // generate a verification code
{
Int number;
Char code;
String checkcode = string. empty;
System. Random random = new random ();
For (INT I = 0; I <4; I ++)
{
Number = random. Next ();
If (Number % 2 = 0)
Code = (char) ('0' + (char) (Number % 10 ));
Else
Code = (char) ('A' + (char) (number % 26 ));
CheckCode + = code. ToString ();
}
Session ["CheckCode"] = checkCode;
Return checkCode;
}
Private void CreateCheckCodeImage (string checkCode) // used to generate a background image
{
If (checkCode = null | checkCode. Trim () = String. Empty)
Return;
System. Drawing. bitmap image = new Bitmap (150, 50 );
Graphics G = graphics. fromimage (image );
Try
{
// Generate the generator immediately;
Random random = new random ();
// Clear the background color of the image
G. Clear (color. White );
// Background noise line for drawing
For (INT I = 0; I <25; I ++)
{
Int x1 = random. Next (image. Width );
Int x2 = random. Next (image. Width );
Int y1 = random. Next (image. Width );
Int y2 = random. Next (image. Width );
G. DrawLine (new Pen (Color. Silver), x1, y1, x2, y2 );
}
Font font = new system. Drawing. Font ("Arial", 40, (system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic ));
System. Drawing. drawing2d. lineargradientbrush brush = 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 of the image
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 ()));
}
// Foreground noise of the image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
System. IO. MemoryStream MS = 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 ();
}
}