In fact, the article on the production of ASP.net verification code has a lot of, but today still want to continue to share with you, pro, you can synthesize several instances, to write the application of their own website asp.net verification code, probably also on two major parts:
First create a asp.net form validatecode.aspx; don't write anything. Write the following code directly in the background ValidateCode.aspx.cs:
protected void Page_Load (object sender, EventArgs e) {string valid Atecode = Createvalidatecode ()//Generate verification code Bitmap Bitmap = new Bitmap (imgwidth,imgheight);//Generate Bitmap image Disturbbi TMap (bitmap); Image background Drewvalidatecode (bitmap,validatecode);//Draw captcha image bitmap. Save image, wait output} private int codelen = 4;//authentication code length private int fine Response.outputstream,imageformat.gif ness = 85;//picture definition private int imgwidth = 48;//picture width private int imgheight = 24;//picture height private string fontfam ily = "Times New Roman";/font name private int fontsize = 14;//font size//private int fontstyle = 0;//font style private int posx = 0;//Draw start coordinates x private int posy = 0;//Draw coordinates y private string Createvalidatecode ()//generate Authenticode {str
ing validatecode = ""; Random Random = new Random ();//Random Number object for (int i = 0; i < Codelen; i++)/loop generate per bit value {int n = Rando M.next (10);//Digital VALidatecode + + n.tostring ();
session["Vcode"] = validatecode;//Save Authenticode This session is called in the foreground. Return validatecode;//back validation code} private void Disturbbitmap (Bitmap Bitmap)//image background {Random Random = new Random ();//generate for (int i = 0; i < bitmap) by random number. Width; i++)//By looping nesting, generate {for (int j = 0; j < bitmap) pixel by point. Height; J + +) {if (random). Next () <= this.fineness) bitmap.
SetPixel (i, J, Color.lightgray); The private void Drewvalidatecode (Bitmap Bitmap, String validatecode)//Draw the captcha image {Graphics g = g Raphics. FromImage (bitmap)//Get the Paint object font font = new Font (fontFamily, fontsize, fontstyle.bold);//Set Drawing font g.drawstring (Validatecode, Font, Brushes.black, Posx, posy);//Draw Authenticode image}
The following diagram features are implemented in the Login.aspx form page:
Login.aspx Form Front Desk:
This function is to click on the verification code picture will replace the verification code
//can use Microsoft's own jqury.js below the jquery-1.4.1.min.js version. Or you can download it on the jquery official web site.
<script src= "styles/jquery-1.4.1.min.js" type= "text/javascript" ></script>
function f_ Refreshtype () {
var Image1 = document.getelementbyidx_x_x_x ("img");
if (Image1!= null) {
IMAGE1.SRC = image1.src + "?";
}
}
---//call function to implement replacement verification code
Background code: Click the login to verify whether the user entered correctly.
String usercode = Txtcode. Text.trim ();
if (Usercode = = session["Vcode"). ToString ())//session["Vcode"]
{
}
Other code is the same as the other.
The above is to share with you about the process of generating asp.net verification code, I hope that we can apply.