1. The event code is first in the Page_Load of the ASPX file in the background verification code:
Copy Code code as follows:
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 Examination system
{
public partial class AjaxAutoCode:System.Web.UI.Page
{
Verifying numbers
public string Authcode = string. Empty;
protected void Page_Load (object sender, EventArgs e)
{
#region the first way to generate a validation code
Random Random = new Random ();
Authcode = random. Next (1111, 9999). ToString ();
Construct a picture
Bitmap image = New Bitmap (Authcode. Length * 12, 25);
Create 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 of the 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, followed by the display of verification code page to define a JS function
Copy Code code as follows:
function Fgetcode ()
{
document.getElementById ("GetCode"). src= "Default2.aspx?" +math.random ();
}
3. Then edit the front page aspx, below is the code snippet for the front page
Copy Code code as follows:
<label> Verification Code </label>
<asp:textbox id= "Txt_checkcode" runat= "Server" width= "178px" ></asp:TextBox>
<a href=" Javascript:fgetcode () "> Replace Verification Code </a>