1. First, the event code in the Page_Load of the ASPX file for the background verification code:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Drawing; namespace student Online Exam system {public partial class AjaxAutoCode:System.Web.UI.Page {//Verify digital public string Authcode = string. Empty; protected void Page_Load (object sender, EventArgs e) {#region The first method of generating a verification code random random = new random (); authcode = random . Next (1111, 9999). ToString (); Constructs the picture Bitmap image = New Bitmap (Authcode. Length * 12, 25); Create a canvas Graphics g = graphics.fromimage (image); try {g.clear (color.white); for (int i = 0; i <; i++) {int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); Link two-point line G.drawline (new Pen (color.silver), x1, y1, x2, y2); } font font = new Font ("Arial", FontStyle.Bold | Fontstyle.italic); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rectangle (0, 0, image. Width, image. Height), Color.Blue, Color.darkblue, 1.2f, true); g.DrawString (Authcode, Font, brush, 2, 2); Picture foreground noise for (int i = 0; i < i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); } g.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); System.IO.MemoryStream ms = new System.IO.MemoryStream (); Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); Ms. WriteTo (this. Response.outputstream); Ms. Close (); This. Response.ContentType = "Image/gif"; } finally {image. Dispose (); G.dispose (); } #endregion}}}
2, next in the Display verification code page defines a JS function
function Fgetcode () {document.getElementById ("GetCode"). src= "Default2.aspx?" +math.random (); }
3. Then edit the foreground page aspx, below is the code snippet for the foreground page
<label> Verification Code </label> <asp:textbox id= "Txt_checkcode" runat= "Server" width= "178px" ></asp:textbox > <a href=" Javascript:fgetcode () "> Replacement Verification Code </a>