/* Copyright All (c) 2005 zhongfeng, http://blog.csdn.net/SW515 */
Public class validatecode: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
This. createcheckcodeimage (generatecheckcode ());
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private string generatecheckcode ()
{
Int number;
Char code;
String checkcode = string. empty;
System. Random random = new random ();
For (INT I = 0; I <5; I ++)
{
Number = random. Next ();
If (Number % 2 = 0)
Code = (char) ('0' + (char) (Number % 10 ));
Else
Code = (char) ('A' + (char) (Number % 26 ));
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 a random Generator
Random random = new random ();
// Clear the background color of the image
G. Clear (color. White );
// Draw the background noise line of the image
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. 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 ()));
}
// Draw the border line 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 ();
}
}
}
Assume that the above verification code generator page is named: checkcode. aspx, use the "
Use the following code to determine the verification code in the event handling button on the logon page:
Private void btnlogin_click (Object sender, system. Web. UI. imageclickeventargs E)
{
If (request. Cookies ["checkcode"] = NULL)
{
Lblmessage. Text = "your browser settings have been disabled for cookies. You must set the options that allow the browser to use cookies before using the system. ";
Lblmessage. Visible = true;
Return;
}
If (string. Compare (request. Cookies ["checkcode"]. Value, txtcheckcode. Text, true )! = 0)
{
Lblmessage. Text = "Incorrect verification code. Enter the correct verification code. ";
Lblmessage. Visible = true;
Return;
}
/****** Other code ******/
}
Source: http://blog.csdn.net/cctaiyang/archive/2005/12/28/563678.aspx