ASP. NET generate Captcha code (pure Chinese)

Source: Internet
Author: User
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 a reference to///<summary>///CHECKCODE_CH///</summary> public class Checkcode_ch {public checkcode_ch () {  TODO: Add constructor logic here//} private static object[] Createstring () {//define an array to store Chinese character coding constituent elements 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 (); Define a random Number object object[] bytes = new Object[4]; for (int i = 0; i < 4; i++) {//Get location Code First bit int ran1 = ran. Next (11, 14); String str1 = Str[ran1]. Trim (); Get Location code second bit and prevent data duplication ran = new Random (Ran1 * Unchecked ((int) DateTime.Now.Ticks) + i); int ran2; if (Ran1 = =) {Ran2 = ran. Next (0, 7); } else {ran2 = ran. Next (0, 16); } string str2 = Str[ran2]. Trim (); Get Location Code third bit Ran = new Random (ran2 * Unchecked ((int) DateTime.Now.Ticks) + i); int ran3 = ran. Next (10, 16); String str3 = Str[ran3]. Trim (); Get Location code Fourth bit ran = new Random (RAN3 * Unchecked ((int) DateTime.Now.Ticks) + i); int ran4; if (ran3 = =) {Ran4 = ran. Next (1, 16); } else if (ran3 = =) {Ran4 = ran. Next (0, 15); } else {Ran4 = ran. Next (0, 16); } string STR4 = Str[ran4]. Trim (); Defines the random 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 resulting kanji byte into the array bytes. SetValue (Stradd, i); } return bytes; } private static String GetString () {Encoding GB = encoding.getencoding ("gb2312"); object[] bytes = createstring ();//According to Han Word byte decodes the Chinese character 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; HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", str)); return str; } public static void Graphicsimage () {System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling (( GetString (). Length * 22.5)), 22); Graphics g = graphics.fromimage (image);  Create canvas try {//Generate random generator randomly random = new random ();//Empty picture background color g.clear (color.white);//Picture background noise line 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", 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 (), Font, BrusH, 2, 2); Picture the foreground noise point for (int i = 0; i < 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 picture 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); HttpContext.Current.Response.ClearContent (); HttpContext.Current.Response.ContentType = "Image/gif"; HttpContext.Current.Response.BinaryWrite (Ms. ToArray ()); } catch (Exception ms) {HttpContext.Current.Response.Write (Ms. Message); } } }

The second step is to set up a page reference class library chinesecheckcode.aspx front desk does not write code, background reference class library.

Using System; Using System.Collections; Using System.Configuration; Using System.Data; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.HtmlControls; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Public partial class UserValidator_ChineseCheckCode:System.Web.UI.Page {protected void Page_Load (object sender, Eventa RGS e) {checkcode_ch.graphicsimage ();//Call method to generate four-bit Kanji Verification Code}}

The third step refers to the verification code page

<asp:textbox id= "Validator" runat= "Server" width= "150px" ></asp:TextBox>  <asp:imagebutton id=" imgbtnlogin "runat=" Server "imageurl=" ~/images/login.gif " onclick= "Imgbtnlogin_click"/>

Background judgment

protected void Imgbtnlogin_click (object sender, ImageClickEventArgs e) {HttpCookie cookie = request.cookies["Checkcode" ]; if (cookie. Value = = this. Validator.Text.Trim ()) {//... } else {Response.Write ("<script>alert (' captcha input error, please re-enter! '); location= ' chinesecodevalidator.aspx ' </script> '); Return } }

The above verification code generates four bits, please make appropriate changes according to the situation.
This paper summarizes the verification code technology for generating pure digital, mixed digital letters and pure Chinese characters. I hope to help you.

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.