Site Verification code is a very common technique. Let me now describe how the technology is implemented
The verification code is a picture. We need to write a section of <IMG>,SRC in the foreground code to point to a page (validateimage.aspx). Code as follows: <script language= "JavaScript" > function changeimg () { $ ("#imgCheckNo"). attr ("src", "validateimage.aspx?r=" + getrandom (999)); } function getrandom (n) {return Math.floor (math.random () * n + 1) } </script> <div> <img id= "Imgcheckno" src= validateimage.aspx "style=" Border-color: #000000; Border-width:1px;border-style:solid "> <span onclick=" changeimg (); > Can't see clearly? change a </span> </div> <div> <div class= "LABELCSS" > Verification Code:</div> <div> <asp:textbox id= "Tbxcheckno" runat= "Server" cssclass= "Tbxcss" ></asp:TextBox> </div> <div> <asp:button id= "btnsubmit" runat= "Server" text= "Register Now" onclick= "btnSubmit_Click"/> </div> validateimage.aspx is used to produce captcha pictures andSave the code value of the CAPTCHA to a cookie. Code as follows: Code as follows: public partial class ValidateImage:System.Web.UI.Page { Pro tected void Page_Load (object sender, EventArgs e) { This. Createcheckcodeimage (Rndnum ()); } ///<summary> generate verification code /// </summary> ///<returns></returns> Private Strin G Rndnum () { int number; Char code; string checkcode = String.Empty; System.Random Random = new Random (); for (int i = 0; i < 4; i++) { &NBsp number = random. Next (); if (number% 2 = 0) &NB Sp code = (char) (' 0 ' + (char) (number% 10)); else &NBSP ; Code = (char) (' A ' + (char) (number% 26); Checkcode + = code. ToString (); RESPONSE.COOKIES.ADD (new HttpCookie ( "Yzmcode", Checkcode)); return checkcode; } ///<summary> page generation verification Code gif picture &NB Sp </summary> ///<param name= "Checkcode" ></param> &NB Sp 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 * 12.5)), 22); Graphics g = graphics.fromimage (image); try { &NBS P //Generate random generators Random Random = new Random (); //Clear picture background color G.clear (Color.White); //Picture background noise lines &NBSP ; for (int i = 0; i < i++) &NBSp { 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); F ont font = new System.Drawing.Font (Arial), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.dr Awing. Drawing2d.lineargradientbrush (New Rectangle (0, 0,Image. Width, image. Height), Color.Blue, color.darkred, 1.2f, true); g.drawstring (checkcode, Font, brush, 2, 2); //Picture foreground noise point &NBSP ; for (int i = 0; i < i++) { &NB Sp int x = random. Next (image. Width); int y = random. Next (image. Height); image. SetPixel (x, Y, Color.FromArgb) (random. Next ())); /Picture border Line G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); &NBSp 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 () &N Bsp image. Dispose (); { } finally Background event to determine if the current verification code is correct. The code is as follows: The code is as follows: #region page control event protected void btnSubmit_Click (object sender, EventArgs e) { String.Compare (request.cookies[) Yzmcode "]. Value, Tbxcheckno.text, True)!= 0 { Response.Write ("<script>alert (' captcha error! ') </script> "); else &NB Sp { //Response.Write ("<script>alert Verify right!!! ') </script> "); #endregion