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/