Register.aspx
When you click on the verification code picture, automatically redirect the authcode.aspx once, then refresh the verification code again
$ (' #authimage '). Click (function () {
$ (this). attr ("src", "authcode.aspx");
});
Verification code: <input id= "Authcode" type= "text" class= "required" name= "Authcode"/>
Note that the path to this captcha image is a dynamic page. We will use GDI + technology to draw the verification code in this dynamic page.
Authcode.cs
public partial class Authcode:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Characters that may appear in the Verification code
String authcodestring = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Verification code Character Set combination length
int length = Authcodestring.length;
Draw character fonts
Font f = new Font ("XXFarEastFont-Arial", fontstyle.bold);
Draw the object of the brush for the verification code
Brush B = null;
Paint the color of the verification code
Color BrushColor = new color ();
Bitmap image = New Bitmap (80, 40);
Graphics g = graphics.fromimage (image);
G.clear (Color.gray);/Set Background
String Authcode = String. empty;//the entire validation code displayed to the user
String code = String. Empty; The currently drawn verification code
Random Random = new Random ();
for (int i = 0; i < 4; i++)
{
The remainder guarantees that the current length does not exceed the verification code character set length
int current = random. Next ((DateTime.Now.Millisecond)% length);
Verification code character set with arbitrary interception of one character
Code = authcodestring.substring (current, 1);
Authcode + = code;
BrushColor = Color.FromArgb (random. Next (255), Random. Next (255), Random. Next (255));
b = new SolidBrush (brushcolor);
Draw the string you just got
g.DrawString (code, F, B, I * 15, 2);
}
Response.Clear ();
Response.ContentType = "Image/pjpeg";
To save an object to the response output stream
Image. Save (Response.outputstream, imageformat.jpeg);
Image. Dispose ();
session["Authcode"] = Authcode; Save the validation code on the server side to compare
Response.End ();
}
}