Asp.net Chinese Verification Code

Source: Internet
Author: User

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. drawing;
Using system. text;
Public partial class admin_validcode: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
This. createcheckcodeimage (generatecheckcode ());
}
Private string generatecheckcode ()
{
Int number;
// Char code;
String checkcode = string. empty;
System. Random random = new random ();
Encoding GB = encoding. getencoding ("gb2312 ");
// Call the function to generate four random Chinese character codes
Object [] bytes = createregioncode (6 );
// 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
Checkcode = str1 + str2 + str3 + str4;
// 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 ();
//}
Response. Cookies. Add (New httpcookie ("validcode", ljflib. encrypt. AES. encryptaes (checkcode )));
// Session ["validcode"] = checkcode;

Return checkcode;
}
# Region generates a waveform filter effect
Private 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];
/* Generate a hexadecimal byte array containing two elements at a time in each loop, and put 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
// Seed 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 const double Pi = 3.1415926535897932384626433832795;
Private const double pi2= 6.283185307179586476925286766559;
Private system. Drawing. Bitmap twistimage (Bitmap srcbmp, bool bxdir, double dmultvalue, double dphase)
{
System. Drawing. Bitmap destbmp = new Bitmap (srcbmp. Width, srcbmp. Height );
// Fill the bitmap background in white
System. Drawing. Graphics graph = system. Drawing. Graphics. fromimage (destbmp );
Graph. fillrectangle (New solidbrush (system. Drawing. color. White), 0, 0, destbmp. Width, destbmp. Height );
Graph. Dispose ();
Double dbaseaxislen = bxdir? (Double) destbmp. Height: (double) destbmp. width;
For (INT I = 0; I <destbmp. width; I ++)
{
For (Int J = 0; j <destbmp. height; j ++)
{
Double dx = 0;
DX = bxdir? (Pi2 * (double) J)/dbaseaxislen: (Pi2 * (double) I)/dbaseaxislen;
Dx + = dphase;
Double DY = math. Sin (dx );
// Obtain the color of the current vertex
Int noldx = 0, noldy = 0;
Noldx = bxdir? I + (INT) (dy * dmultvalue): I;
Noldy = bxdir? J: J + (INT) (dy * dmultvalue );
System. Drawing. color = srcbmp. getpixel (I, j );
If (noldx> = 0 & noldx <destbmp. Width
& Amp; noldy & gt; = 0 & amp; noldy <destbmp. Height)
{
Destbmp. setpixel (noldx, noldy, color );
}
}
}
Return destbmp;
}
# Endregion
Private void createcheckcodeimage (string checkcode)
{
If (checkcode = NULL checkcode. Trim () = string. Empty)
Return;
System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 27.0), 25 );
Graphics G = graphics. fromimage (image );
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 <12; 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. Silver), X1, Y1, X2, Y2 );
}
// Font font = new system. Drawing. Font ("Arial", 16, (system. Drawing. fontstyle. Bold system. Drawing. fontstyle. italic ));
Font font = new system. Drawing. Font (" _ gb2312", 16, (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 (checkcode, Font, brush, 2, 2 );
// Foreground noise of the image
For (INT I = 0; I <100; I ++)
{
Int x = random. Next (image. width );
Int y = random. Next (image. Height );
Image. setpixel (X, Y, color. fromargb (random. Next ()));
}
// Image = twistimage (image, true, 3, 1 );
// Draw the waveform filter effect of the image
// 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 ());
}
Finally
{
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.