Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
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;
Using System. Drawing;
Public partial class CnCode: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Obtain the GB2312 encoding page (table)
Encoding gb = Encoding. GetEncoding ("gb2312 ");
// Call the function to generate four random Chinese character codes
Object [] bytes = CreateRegionCode (4 );
// Decodes Chinese Characters Based on the byte array encoded by 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 []);
// Output Console
CreateImage (str1 + str2 + str3 + str4 );
}
/*
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
The four byte arrays are stored in the object array.
Parameter: strlength, indicating the number of Chinese characters to be generated
*/
Public static object [] CreateRegionCode (int strlength)
{
// Define a string array to store the components of Chinese character encoding
String [] rBase = new String [16] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "a", "B", "c", "d", "e", "f "};
Random rnd = new Random ();
// Define an object array
Object [] 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
Int r1 = rnd. Next (11,14 );
String str_r1 = rBase [r1]. Trim ();
// Code 2nd bits
Rnd = new Random (r1 * unchecked (int) DateTime. Now. Ticks) + I); // Replace the seed of the Random number generator to avoid repeated values.
Int r2;
If (r1 = 13)
{
R2 = rnd. Next (0, 7 );
}
Else
{
R2 = rnd. Next (0, 16 );
}
String str_r2 = rBase [r2]. Trim ();
// Code 3rd bits
Rnd = new Random (r2 * unchecked (int) DateTime. Now. Ticks) + I );
Int r3 = rnd. Next (10, 16 );
String str_r3 = rBase [r3]. Trim ();
// Code 4th bits
Rnd = new Random (r3 * unchecked (int) DateTime. Now. Ticks) + I );
Int r4;
If (r3 = 10)
{
R4 = rnd. Next (1, 16 );
}
Else if (r3 = 15)
{
R4 = rnd. Next (0, 15 );
}
Else
{
R4 = rnd. Next (0, 16 );
}
String str_r4 = rBase [r4]. Trim ();
// Define the random Chinese character location code generated by storing two byte Variables
Byte byte1 = Convert. ToByte (str_r1 + str_r2, 16 );
Byte byte2 = Convert. ToByte (str_r3 + str_r4, 16 );
// Store two byte variables in the byte array
Byte [] str_r = new byte [] {byte1, byte2 };
// Put the byte array of the generated Chinese character into the object Array
Bytes. SetValue (str_r, I );
}
Return bytes;
}
Private void CreateImage (string checkCode)
{
Int iwidth = (int) (checkCode. Length * 25 );
System. Drawing. Bitmap image = new System. Drawing. Bitmap (iwidth, 20 );
Graphics g = Graphics. FromImage (image );
Font f = new System. Drawing. Font ("Arial", 12, System. Drawing. FontStyle. Bold );
Brush B = new System. Drawing. SolidBrush (Color. White );
// G. FillRectangle (new System. Drawing. SolidBrush (Color. Blue), 0, 0, image. Width, image. Height );
G. Clear (Color. Blue );
G. DrawString (checkCode, f, B, 3, 3 );
Pen blackPen = new Pen (Color. Black, 0 );
Random rand = new Random ();
For (int I = 0; I <4; I ++)
{
Int y = rand. Next (image. Height );
G. DrawLine (blackPen, 0, y, image. Width, y );
}
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
Response. ClearContent ();
Response. ContentType = "image/png ";
Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Image. Dispose ();
}
}
This article is reprinted from 'Big intelligence blog ': http://www.csafe.cn/article.asp? Id = 1274