Using system;
Using system. Data;
Using system. configuration;
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. Text; // Add reference
Using system. Drawing; // Add reference
Public partialclass
_ Default: system. Web. UI. Page
{
Protected voidpage_load (Object
Sender, eventargs E)
{
Graphicsimage (4); // call a method to generate a four-digit Chinese Character Verification Code
}
Private object [] createstring (intstrlength)
{
// Define an array to store the components of Chinese character encoding
String [] STR = new string [16] {"0", "1", "2", "3", "4 ",
"5", "6", "7", "8", "9 ",
"A", "B", "C", "D ",
"E", "F "};
Random
Ran = new random (); // defines a random number object
Object [] bytes =
New object [strlength];
For (int
I = 0; I <strlength; I ++)
{
// Obtain the first location code
Int ran1 = ran. Next (11, 14 );
String str1 = STR [ran1]. Trim ();
// Obtain the second location code and prevent data duplication
Ran = new random (ran1 *
Unchecked (INT) datetime. Now. ticks) + I );
Int ran2;
If (ran1 = 13)
{
Ran2 = ran. Next (0, 7 );
}
Else
{
Ran2 = ran. Next (0, 16 );
}
String str2 = STR [ran2]. Trim ();
// Obtain the third digit of the Location Code
Ran = new random (ran2 * unchecked (INT) datetime. Now. ticks) + I );
Int ran3 = ran. Next (10, 16 );
String str3 = STR [ran3]. Trim ();
// Obtain the fourth place of the Location Code
Ran = new random (ran3 * unchecked (INT) datetime. Now. ticks) + I );
Int ran4;
If (ran3 = 10)
{
Ran4 = ran. Next (1, 16 );
}
Else if (ran3 = 15)
{
Ran4 = ran. Next (0, 15 );
}
Else
{
Ran4 = ran. Next (0, 16 );
}
String str4 = STR [ran4]. Trim ();
// Define the random Chinese character location code generated by BYTE Variable Storage
Byte byte1 =
Convert. tobyte (str1 + str2, 16 );
Byte byte2 =
Convert. tobyte (str3 + str4, 16 );
Byte [] stradd = new
Byte [] {byte1, byte2 };
// Put the Generated Chinese Character bytes into an array
Bytes. setvalue (stradd, I );
}
Return bytes;
}
Private string getstring (INT length)
{
Encoding GB =
Encoding. getencoding ("gb2312 ");
Object [] bytes = createstring (length );
// Decodes Chinese Characters Based on Chinese Characters
String str1 = GB. getstring (byte []) convert. changetype (Bytes [0], typeof (byte []);
String str2 = GB. getstring (byte []) convert. changetype (Bytes [1], typeof (byte []);
String str3 = GB. getstring (byte []) convert. changetype (Bytes [2], typeof (byte []);
String str4 = GB. getstring (byte []) convert. changetype (Bytes [3], typeof (byte []);
String
STR = str1 + str2 + str3 + str4;
Response. Cookies. Add (New httpcookie ("checkcode", STR ));
Return STR;
}
Private voidgraphicsimage (int
Length)
{
System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (getstring (length). length * 22.5 )),
22 );
Graphics G =
Graphics. fromimage (image); // create a canvas
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 (INTI = 0; I <1; 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 (newpen (color. Black), X1, Y1, X2, Y2 );
}
Font
Font = new system. Drawing. Font ("couriew new", 12, system. Drawing. fontstyle. Bold );
System. Drawing. drawing2d. lineargradientbrushbrush =
New system. Drawing. drawing2d. lineargradientbrush
(New rectangle (0, 0, image. Width, image. Height), color. Blue,
Color. darkred, 1.2f, true );
G. drawstring (getstring (length), Font, brush, 2, 2 );
// Foreground noise of the image
For (INTI = 0; I <50; 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 ());
}
Catch (exceptionms)
{
Response. Write (Ms. Message );
}
}
}