ASP. NET Captcha Image Generation Example

Source: Internet
Author: User
The verification code is a picture. We need to write a paragraph in the foreground code <IMG>,SRC point to a page (validateimage.aspx).

<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> <span onclick=" changeimg (); " > not clear? change </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 images, and the code values of the verification code are saved to the cookie.
The code is as follows:

Public partial class ValidateImage:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {this.        Createcheckcodeimage (Rndnum ()); }///<summary> Generate verification Code///</summary>//<returns></returns> Private St            Ring Rndnum () {int number;            char code;            string checkcode = String.Empty;            System.Random random = new Random (); for (int i = 0; i < 4; 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 ("Yzmcode", Checkcode));        return checkcode; }///<summary> generate CAPTCHA GIF images to page///</summary>//<param name="Checkcode" ></param> 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 {//Generate random generator randomly random = new random ();                Empty the picture background color g.clear (color.white); Picture the background noise line for (int i = 0; i < 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", and (System.Drawing.FontStyle.bold |                System.Drawing.FontStyle.Italic)); 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); 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);                Response.clearcontent ();                Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms. ToarrAy ());                } finally {g.dispose (); Image.            Dispose (); }        }    }

The last is the background event, to determine whether the current verification code is correct.
The code is as follows:

#region Page control event        protected void btnSubmit_Click (object sender, EventArgs e)        {            if (String.Compare ( request.cookies["Yzmcode"]. Value, Tbxcheckno.text, true)! = 0)            {                Response.Write ("<script>alert (' captcha is wrong! ') </script> ");            }            else            {               //Response.Write (' <script>alert (' verify correct!!! ') </script> ");            }        }  #endregion
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.