GDI + generate Verification Code

Source: Internet
Author: User

Register. aspx
 
// When you click the verification code image, the authcode. aspx is automatically redirected and the verification code is refreshed again.
$ ('# Authimage'). click (function (){
$ (This). attr ("src", "authcode. aspx ");
});
 
 
Verification Code: <input id = "authcode" type = "text" class = "required" name = "authcode"/>

Note: the path of the Verification Code image is a dynamic page! We use the GDI + technology to draw verification codes on this dynamic page.
 
Authcode. cs
Public partial class authcode: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Possible characters in the verification code
String authCodeString = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
// Character set length of the Verification Code
Int length = authCodeString. Length;
// Draw Character Font
Font f = new Font ("", 24, FontStyle. Bold );
// Specifies the image painter who draws the verification code.
Brush B = null;
// Color of the Verification Code
Color brushColor = new Color ();
Bitmap image = new Bitmap (80, 40 );
Graphics g = Graphics. FromImage (image );
G. Clear (Color. Gray); // sets the background.

String authCode = string. Empty; // The verification 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 ensures that the current length does not exceed the combined length of the Verification Code Character Set
Int current = random. Next (DateTime. Now. Millisecond) % length );

// The Character Set of the verification code can be used to intercept any 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 obtained
G. DrawString (code, f, B, I * 15, 2 );
}
Response. Clear ();
Response. ContentType = "image/pjpeg ";
// Save the object to the Response output stream
Image. Save (Response. OutputStream, ImageFormat. Jpeg );
Image. Dispose ();
Session ["authCode"] = authCode; // Save the verification code on the server side for comparison
Response. End ();
}
}

From Xu Yue's column

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.