. NET Chinese and English mixed authentication Code implementation Codes _ Practical skills

Source: Internet
Author: User

The final effect is as shown:

The CheckCode.aspx.cs code is as follows

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Get GB2312 Encoding page (table)
/**//**
* The method used to generate verification code in Chinese
* Note, to generate a Chinese verification code to change the width of the generated verification code picture
* var imagecode = new System.Drawing.Bitmap (int) math.ceiling (code. Length * 22.5)), 23); Define the width and height of the picture
**/

var GB = encoding.getencoding ("gb2312");

/**/////Call function produces 4 random Chinese encoding
object[] bytes = Createregioncode (4);

/**/////decoding Chinese characters according to the encoding byte array
var sbcode = new StringBuilder (). Append (GB. GetString ((byte[]) Convert.changetype (bytes[0), typeof (byte[)))
// . Append (GB. GetString ((byte[]) Convert.changetype (bytes[1), typeof (byte[)))
// . Append (GB. GetString ((byte[]) Convert.changetype (bytes[2), typeof (byte[)))
// . Append (GB. GetString ((byte[]) Convert.changetype (bytes[3), typeof (byte[)));
Createcheckcodeimage (Sbcode.tostring ());

Createcheckcodeimage (Generatecheckcode ()); The method to use to generate the digital English
}

Generating Chinese character verification code #region generating Chinese character verification code
/**////<summary>
This function randomly creates a hexadecimal byte array with two elements in the range of encoding, each byte array represents a Chinese character, and four byte arrays are stored in an object array.
</summary>
<param name= "Strlength" > represents the number of Chinese characters to be produced </param>
<returns></returns>
Static object[] Createregioncode (int strlength)
{
var rbase = new[]
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"
};

var random = new Random ();
var 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
var r1 = random. Next (11, 14);
var str_r1 = Rbase[r1]. Trim ();

Random = new Random (R1 * Unchecked ((int) DateTime.Now.Ticks) + i); Replace the seed of the random number generator to avoid duplicate values

var r2 = 0;
if (r1 = 13)
r2 = random. Next (0, 7);
Else
r2 = random. Next (0, 16);

var str_r2 = rbase[r2]. Trim ();

Location Code 3rd place
Random = new Random (R2 * Unchecked ((int) DateTime.Now.Ticks) + i);
var r3 = random. Next (10, 16);
var str_r3 = Rbase[r3]. Trim ();

Location Code 4th place
Random = new Random (R3 * Unchecked ((int) DateTime.Now.Ticks) + i);
var r4 = 0;
if (R3 = 10)
R4 = random. Next (1, 16);
else if (r3 = 15)
R4 = random. Next (0, 15);
Else
R4 = random. Next (0, 16);

var str_r4 = Rbase[r4]. Trim ();

var byte1 = convert.tobyte (str_r1 + str_r2, 16);
var byte2 = convert.tobyte (Str_r3 + STR_R4, 16);
Storing two byte variables in a byte array
var str_r = new[] {byte1, byte2};

Puts a byte array of the resulting Chinese character into an object array
bytes. SetValue (Str_r, i);
}
return bytes;
}
#endregion

Generate pictures #region Generate pictures
void Createcheckcodeimage (String code)
{
var imagecode = new System.Drawing.Bitmap (int) math.ceiling (code. Length * 12.5)), 23); Define the width and height of the picture
var g = graphics.fromimage (Imagecode); Load the picture onto the canvas

Try
{
var random = new Random ();
G.clear (Color.White); Empty picture background color

Background noise line for painting pictures
for (int i = 0; i < i++)
{
var x1 = random. Next (Imagecode.width);
var x2 = random. Next (Imagecode.width);
var y1 = random. Next (Imagecode.height);
var y2 = random. Next (Imagecode.height);

G.drawline (New Pen (Color.silver), new Point (x1, y1), New Point (x2, y2));
}

var font = new System.Drawing.Font ("Arial", 12F, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
var brush = new System.Drawing.Drawing2D.LinearGradientBrush (
New Rectangle (0, 0, Imagecode.width, imagecode.height),
Color.Blue, color.darkred, 1.2F, true);
g.DrawString (code, font, Brush, 2, 2);

Picture of the foreground noise point
for (int i = 0; i < i++)
{
var x = random. Next (Imagecode.width);
var y = random. Next (Imagecode.height);
Imagecode.setpixel (x, Y, Color.FromArgb) (random. Next ()));
}

Draw a picture's border line
G.drawrectangle (New Pen (Color.silver), 0, 0, imagecode.width-1, imagecode.height-1);
var ms = new System.IO.MemoryStream ();
Imagecode.save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.clearcontent ();
Response.ContentType = "Image/jpeg";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Imagecode.dispose ();
}

}
#endregion

Generate Data verification Code #region generate data verification code
private String Generatecheckcode ()
{
int number;
char code;
string checkcode = String.Empty;

var random = new Random ();

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 ();
}
return checkcode;
}
#endregion


To use the Authentication code page:


Copy Code code as follows:

<form id= "FormCode" runat= "Server" >
<div>

</div>
</form>

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.