Asp. How to generate verification code dynamically in net

Source: Internet
Author: User

Now many websites have used the technology of verification Code, the implementation is also a variety of ways, here mainly introduced in asp.net can be used in a dynamic generation of verification code method, may not be very perfect, but the implementation of the difficulty is lower.

This method utilizes the common dynamic picture generation technique, but the more special point is that the image generation is executed in the Page_Load method of a page type subclass. So the contenttype of response is image/gif, not text/html.

GraphicalText.aspx.cs Code:

 using System;  
Using System.Drawing;  
       
Using System.Drawing.Imaging; namespace WebApplication1 {public partial class GraphicalText:System.Web.UI.Page {protected VO  
            ID Page_Load (object sender, EventArgs e) {using (Bitmap image = new Bitmap (30, 20)) {using (Graphics g = graphics.fromimage (image)) {G.fillrectan  
                    GLE (brushes.yellow, 0, 0, 30, 20);  
                    G.drawrectangle (pens.red, 0, 0, 29, 19);  
                    Font f = new Font ("Arial", 9, fontstyle.italic);  
                    String code = request.querystring["Code"];  
                    g.DrawString (Code, F, brushes.blue, 0, 0);  
                    Response.ContentType = "Image/gif"; Image.  
                Save (Response.outputstream, imageformat.gif); }  
            }  
        }  
    }  
}

Note that you must add this code--"Response.ContentType =" image/gif ";", otherwise it will not display correctly in browsers other than IE.

The corresponding graphicaltext.aspx code is simple, only one line, because there is no need for HTML output:

<%@ Page language= "C #" autoeventwireup= true "codebehind=" GraphicalText.aspx.cs "inherits=" Webapplication1.graphicaltext "%>

The key in the main page is to assign the ImageUrl of the picture to the previous page address, and to make the picture content change, 4-bit random numbers as its parameters.

Default.aspx.cs Code:

Using System;  
Using System.Text;  
       
Namespace WebApplication1  
{public  
    partial class _default:system.web.ui.page  
    {  
        protected void Page_ Load (object sender, EventArgs e)  
        {  
            StringBuilder sb = new StringBuilder ("~/graphicaltext.aspx?code=");  
            Random d = new Random ();  
            Sb. Append ((char) d.next);  
            Sb. Append ((char) d.next);  
            Sb. Append ((char) d.next);  
            Sb. Append ((char) d.next);  
            Image1.imageurl = sb. ToString ();}}}  

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.