GDI + generation Verification code in ASP.net __.net

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.