Generate a Chinese Verification Code (ASP. net c #)

Source: Internet
Author: User

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;

Using System. Text;

Namespace RandomChineseCode
{
/// <Summary>
/// RandomChineseCode abstract description.
/// </Summary>
Public class RandomChineseCode: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page

String checkCode = ConvertChinese (4 );
This. Session ["CheckCode"] = checkCode;
CreateImage (checkCode );
}

# 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

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 string ConvertChinese (int len)
{
// Obtain the GB2312 encoding page (table)
Encoding gb = Encoding. GetEncoding ("gb2312 ");

// Call the function to generate four random Chinese character codes
Object [] bytes = CreateRegionCode (len );

// 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 []);

Return str1 + str2 + str3 + str4;
}

Private void CreateImage (string checkCode)
{
Int iwidth = (int) (checkCode. Length * 18 );
System. Drawing. Bitmap image = new System. Drawing. Bitmap (iwidth, 20 );
Graphics g = Graphics. FromImage (image );
Font f = new System. Drawing. Font ("Arial", 10, 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. Coral );
// Both methods can change the background color of the generated image.


G. DrawString (checkCode, f, B, 3, 3 );

//// The for loop below is used to generate random horizontal lines
// Pen blackPen = new Pen (Color. Tomato, 0 );
// Random rand = new Random ();
// For (int I = 0; I <2; I ++)
//{
// Int y = rand. Next (image. Height );
// G. DrawLine (blackPen, 0, y, image. Width, y); // draw a horizontal line
//}



System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
Response. ClearContent ();
Response. ContentType = "image/Jpeg ";
Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Image. Dispose ();
}
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.