Log.aspx
You 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 website. <script type= "Text/javascript" language= "JavaScript" >//regain validation character function changeimage () {//click Trigger Picture Reload Event, complete the replacement document.getElementById ("IMG") of the image captcha. src = document.getElementById ("img"). src + '? '; Where Imgrandom is the ID of the CAPTCHA picture. } </script>
Log.aspx.cs
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class mb_yanzhengma_log:system.web.ui.page{ protected void Page_Load ( Object sender, EventArgs e) { } protected void Button1_Click (object sender, EventArgs e) { string Usercode = Txtcode. Text.trim (); if (Usercode = = session["Vcode"). ToString ())//session["Vcode"] { Response.Write ("OK, correct"); } else { Response.Write ("Captcha does not match"); } } }
New Validatecode.aspx Page
ValidateCode.aspx.cs
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using system.drawing;using System.drawing.imaging;public Partial class Mb_yanzhengma_ validatecode:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {string Validatecode = Createvalidatecode ();//Generate verification code Bitmap Bitmap = new Bitmap (ImgWidth, imgheight);//Generate Bitmap image Disturbbitmap (bitmap); Image background Drewvalidatecode (bitmap, Validatecode);//Draw the captcha image bitmap. Save (Response.outputstream, imageformat.gif);//preserve image, wait for output} private int codelen = 4;//Authenticode length private int fineness = 85;//picture clarity private int imgwidth = 48;//picture width private int imgheight = 24;//picture height private string fontFamily = "T IMEs 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 ();//Randomize object for (int i = 0; i < Codelen; i++)//loop generates per-digit 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 verification code} private void Disturbbitmap (Bitmap Bitmap)//image Background {random random = new Ran Dom ();//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 = Gr Aphics. FromImage (bitmap);//Gets the drawing object font font = new Font (fontFamily, fontSize, fontstyle.bold);//Set Drawing font G.drawstri Ng (Validatecode, font, Brushes.black, PosX, PosY);//Draw Captcha Image}}
. NET code Small example