asp.net Chinese verification Code, free open Source code _ Practical Tips

Source: Internet
Author: User
Tags rand
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)
{
Get GB2312 Encoding page (table)
Encoding GB = encoding.getencoding ("gb2312");

Call function generates 4 random Chinese encoding
object[] bytes = Createregioncode (4);

Decoding Chinese characters according to the encoding byte array
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[)));

Console for output
CreateImage (str1 + str2 + str3 + STR4);

}

/*
This function randomly creates a hexadecimal byte array with two elements in the range of encoding, each byte array represents a character, and the
A four-byte array is stored in an object array.
Parameters: Strlength, representing the number of characters to be produced
*/
public static object[] Createregioncode (int strlength)
{
Defines a string array that stores the constituent elements of the 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 ();

Defines an object array to
Object[] Bytes=new object[strlength];

/**//* produces a hexadecimal byte array of two elements at a time, and puts it in a bject array
Each Chinese character is composed of four location codes.
Location Code 1th and location code 2nd bit as the first element of a byte array
Location Code 3rd and Location Code 4th digits as the second element of a byte array
*/
for (int i=0;i<strlength;i++)
{
Location Code 1th Place
int R1=rnd. Next (11,14);
String STR_R1=RBASE[R1]. Trim ();

Location Code 2nd place
Rnd=new Random (r1*unchecked (int) DateTime.Now.Ticks) +i)//Replace the seed of the random number generator to avoid duplicate values
int R2;
if (r1==13)
{
R2=rnd. Next (0,7);
}
Else
{
R2=rnd. Next (0,16);
}
String STR_R2=RBASE[R2]. Trim ();

Location Code 3rd place
Rnd=new Random (r2*unchecked (int) DateTime.Now.Ticks) +i);
int R3=rnd. Next (10,16);
String STR_R3=RBASE[R3]. Trim ();

Location Code 4th place
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 ();

Defines a random Chinese character location code generated by two byte variable storage
Byte Byte1=convert.tobyte (str_r1 + str_r2,16);
Byte Byte2=convert.tobyte (STR_R3 + str_r4,16);
Storing two byte variables in a byte array
Byte[] Str_r=new Byte[]{byte1,byte2};

Puts a byte array of the resulting Chinese character into an 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", 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 ();
}

}

The article is reproduced from the ' Great Intelligence Blog ': http://www.csafe.cn/article.asp?id=1274
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.