Draw verification code-Chinese Character Verification Code

Source: Internet
Author: User
The Chinese Character verification code is more advanced than the letter or number verification code, and more effective to prevent illegal registration...

Example:
Add an image control. The HTML attributes are as follows:
<Asp: Image id = "image1" src = "checkcode. aspx" runat = "server" Height = "21px" width = "85px"/>

The source code of the checkcode. aspx file is as follows:

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 partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Graphicsimage (4); // call a method to generate a four-digit Chinese Character Verification Code
}

Private object [] createstring (INT strlength)
{
// 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 void graphicsimage (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 (INT I = 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 (new pen (color. Black), X1, Y1, X2, Y2 );
}

Font font = new system. Drawing. Font ("couriew new", 12, system. Drawing. fontstyle. Bold );
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 (getstring (length), Font, brush, 2, 2 );

// Foreground noise of the image
For (INT I = 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 (exception MS)
{
Response. Write (Ms. Message );
}

}
}
==================================
Http://www.yxsoft.net.cn/article.asp? Id = 12

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.