Protected void page_load (Object sender, eventargs E)
{
This. createcheckcodeimage (generatecheckcode (4 ));
}
Private string generatecheckcode (INT length)
{
// Int number;
// Char code;
// String checkcode = string. empty;
// System. Random random = new random ();
// For (INT I = 0; I <length; 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. Add ("checkcode", checkcode );
/// Response. Cookies. Add (New httpcookie ("checkcode", checkcode ));
// Return checkcode;
String vchar = "2, 3, 4, 6, 7, 8, 9, A, B, C, D, E, F, H, K, M, N" +
", R, S, T, U, V, W, X ";
String [] vcarray = vchar. Split (New char [] {','});
String checkcode = "";
Int temp =-1;
Random Rand = new random ();
For (INT I = 1; I <Length + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (25 );
If (temp! =-1 & temp = T)
{
Return generatecheckcode (length );
}
Temp = T;
Checkcode + = vcarray [T];
}
Session. Add ("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 * 14.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", 14, (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 ();
}
}