Final Effect
Checkcode. aspx. CSCodeAs follows:
Copy code The Code is as follows: protected void page_load (Object sender, eventargs E)
{
// Obtain the gb2312 encoding page (table)
/**//**
* How to generate a Chinese Verification Code
* Note: when generating a Chinese verification code, you must change the width of the image that generates the verification code.
* Var imagecode = new system. Drawing. Bitmap (INT) math. Ceiling (code. length * 22.5), 23); // defines the width and height of the image.
**/
// Var GB = encoding. getencoding ("gb2312 ");
/** // Call the function to generate four random Chinese character codes
// Object [] bytes = createregioncode (4 );
/** // Decodes Chinese characters from the byte array encoded by Chinese Characters
// Var sbcode = new stringbuilder (). append (GB. getstring (byte []) convert. changetype (Bytes [0], typeof (byte [])
//. Append (GB. getstring (byte []) convert. changetype (Bytes [1], typeof (byte [])
//. Append (GB. getstring (byte []) convert. changetype (Bytes [2], typeof (byte [])
//. Append (GB. getstring (byte []) convert. changetype (Bytes [3], typeof (byte []);
// Createcheckcodeimage (sbcode. tostring ());
Createcheckcodeimage (generatecheckcode (); // method used to generate numbers in English
}
Generate a Chinese character Verification Code # region generate a Chinese character Verification Code
/** // <Summary>
/// This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents a Chinese character and stores four byte arrays in the object array.
/// </Summary>
/// <Param name = "strlength"> indicates the number of Chinese characters to be generated. </param>
/// <Returns> </returns>
Static object [] createregioncode (INT strlength)
{
VaR rbase = new []
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9 ", "A", "B", "C", "D", "E", "F"
};
VaR random = new random ();
VaR bytes = new object [strlength];
/** // * Generates a hexadecimal byte array containing two elements at a time in each loop, and puts it into the bject array.
Each Chinese Character consists of four location codes.
The first element of the byte array is the 1st bits and 2nd bits.
The second element of the byte array is the 3rd bits and 4th bits.
*/
For (INT I = 0; I <strlength; I ++)
{
// Code 1st bits
VaR R1 = random. Next (11, 14 );
VaR str_r1 = rbase [R1]. Trim ();
Random = new random (R1 * unchecked (INT) datetime. Now. ticks) + I); // Replace the seed of the random number generator to avoid repeated values.
VaR r2 = 0;
If (r1 = 13)
R2 = random. Next (0, 7 );
Else
R2 = random. Next (0, 16 );
VaR str_r2 = rbase [R2]. Trim ();
// Code 3rd bits
Random = new random (R2 * unchecked (INT) datetime. Now. ticks) + I );
VaR R3 = random. Next (10, 16 );
VaR str_r3 = rbase [R3]. Trim ();
// Code 4th bits
Random = new random (R3 * unchecked (INT) datetime. Now. ticks) + I );
VaR r4 = 0;
If (R3 = 10)
R4 = random. Next (1, 16 );
Else if (R3 = 15)
R4 = random. Next (0, 15 );
Else
R4 = random. Next (0, 16 );
VaR str_r4 = rbase [R4]. Trim ();
VaR byte1 = convert. tobyte (str_r1 + str_r2, 16 );
VaR byte2 = convert. tobyte (str_r3 + str_r4, 16 );
// Store two byte variables in the byte array
VaR str_r = new [] {byte1, byte2 };
// Put the byte array of the generated Chinese character into the object Array
Bytes. setvalue (str_r, I );
}
Return bytes;
}
# Endregion
Generating images # region generating images
Void createcheckcodeimage (string code)
{
VaR imagecode = new system. Drawing. Bitmap (INT) math. Ceiling (code. length * 12.5), 23); // defines the width and height of the image.
VaR G = graphics. fromimage (imagecode); // load the image to the canvas
Try
{
VaR random = new random ();
G. Clear (color. White); // clear the background color of the image.
// Draw the background noise line of the image
For (INT I = 0; I <25; I ++)
{
VaR X1 = random. Next (imagecode. width );
VaR X2 = random. Next (imagecode. width );
VaR Y1 = random. Next (imagecode. Height );
VaR y2 = random. Next (imagecode. Height );
G. drawline (new pen (color. Silver), new point (x1, Y1), new point (X2, Y2 ));
}
VaR font = new system. Drawing. Font ("Arial", 12f, system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic );
VaR brush = new system. Drawing. drawing2d. lineargradientbrush (
New rectangle (0, 0, imagecode. Width, imagecode. Height ),
Color. Blue, color. darkred, 1.2f, true );
G. drawstring (Code, Font, brush, 2, 2 );
// Foreground noise of the image
For (INT I = 0; I <100; I ++)
{
VaR x = random. Next (imagecode. width );
Var y = random. Next (imagecode. Height );
Imagecode. setpixel (X, Y, color. fromargb (random. Next ()));
}
// Draw the border line of the image
G. drawrectangle (new pen (color. Silver), 0, 0, imagecode. Width-1, imagecode. Height-1 );
VaR MS = new system. Io. memorystream ();
Imagecode. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Response. clearcontent ();
Response. contenttype = "image/JPEG ";
Response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Imagecode. Dispose ();
}
}
# Endregion
Generate a data verification code # region generate a data verification code
Private string generatecheckcode ()
{
Int number;
Char code;
String checkcode = string. empty;
VaR 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 ();
}
Return checkcode;
}
# Endregion
Verification code page:
Copy codeThe Code is as follows: <Form ID = "formcode" runat = "server">
<Div>
</Div>
</Form>