In fact, about the production of ASP. NET verification code is a lot of articles, but today still want to share with you, pro, you can synthesize a few examples, to write a Web-based verification code for their own site, probably also two parts:
Create an ASP. validatecode.aspx; do not write anything. Write the following code directly in the background ValidateCode.aspx.cs:
protected void Page_Load (object sender, EventArgs e) {string validatecode = Createvalidatecode ();//Generate Verification code Bitmap b Itmap = new Bitmap (imgwidth,imgheight);//Generate Bitmap Image Disturbbitmap (BITMAP); Image background Drewvalidatecode (bitmap,validatecode);//Draw Verification code image bitmap. Save (response.outputstream,imageformat.gif);//preserve image, wait for output} private int codelen = 4;//captcha length private int fineness = 85;//picture Clear sharpness private int imgwidth = 48;//picture width private int imgheight = 24;//picture height private string fontFamily = "Times New Roman";//font name private int fontSize = 14;//font size//private int fontstyle = 0;//font style private int posX = 0;//Draw starting coordinates x private int posY = 0;/ /Draw coordinates y private string Createvalidatecode ()//Generate verification code {string validatecode = ""; Random random = new random (), or//stochastic object for (int i = 0; i < Codelen; i++)//loop generates each bit value {int n = random. Next (10);//digital Validatecode + = n.tostring (); } session["Vcode"] = validatecode;//Save Verification Code This Session is called at the foreground. Return validatecode;//returns the verification code} private void Disturbbitmap (Bitmap bitmAP)//image Background {random random = new random ();//Generate for by random number for (int i = 0; i < bitmap. Width; i++)//Generate {for (int j = 0; J < bitmap) per pixel by loop nesting. Height; J + +) {if (random. Next (<= this.fineness) bitmap. SetPixel (i, J, Color.lightgray); }}}private void Drewvalidatecode (Bitmap Bitmap, String validatecode)//Draw Authenticode image {Graphics g = graphics.fromimage (Bitmap) ;//Gets the drawing object font font = new Font (fontFamily, fontSize, fontstyle.bold);//Set Drawing font g.drawstring (Validatecode, Font, Brushe S.black, PosX, PosY);//Draw Captcha Image}
Login.aspx Form Front Desk:
This function is in the Click Verification Code picture will replace the verification code//can be used with Microsoft's own jqury.js below the jquery-1.4.1.min.js version. Or you can download it on the jquery official website. <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 Login to verify that the user is entered correctly.
String usercode = Txtcode. Text.trim (); if (Usercode = = session["Vcode"). ToString ())//session["Vcode"] {}
The other code is the same as the others.
The above is to share with you about the process of generating ASP code, I hope you can apply your knowledge.