/ * Copyright all (c) 2005 ZhongFeng, http://blog.9cbs.net/SW515 * / public class ValidateCode: System.Web.UI.Page {private void Page_Load (object sender, System.EventArgs e) {this. CreateCheckcodeImage (GenerateCheckcode ());
#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///
Private string generatecheckcode () {int number; char code; string checkcode = string.empty
System.random random = new random ();
For (int i = 0; i <5; i ) {Number = random.next ();
IF (Number% 2 == 0) Code = (CHAR) ('0' (Number% 10)); Else Code = (CHAR) ('a' (Number% 26));
Checkcode = Code.toString ();
Response.cookies.add (New Httpcookie ("Checkcode", Checkcode);
Return CHECKCODE;
Private void createcheckcodeImage (string checkcode) {if (checkcode == null || Checkcode.trim () == String.empty) Return;
System.drawing.bitmap image = new system.drawing.bitmap ((int) Math.ceiling (Checkcode.length * 12.5)), 22); graphics g = graphics.fromImage (image);
Try {// Generate Random Builder Random Random = New Random ();
// Empty picture background color g.clear (color.white);
/ / Draw picture background noise line for (int i = 0; i <25; 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); g.drawline (new pen (color.silver), x1, y1, x2, y2);}
Font font = new System.Drawing.Font ( "Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D . Lineargradientbrush (New Rectangle (0, 0, Image.width, Image.Height), Color.Blue, Color.darkRed, 1.2F, True; G.DrawString (Checkcode, Font, Brush, 2, 2);
前 图片 点 点 (int = 0; i <100; i ) {INT x = random.next (image.width); int y = random.next (image.height);
Image.setpixel (x, y, color.fromargb (random.next ()));}
// Draw the edge of the picture 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); Response.ClearContent (); Response.ContentType = "image / Gif"; Response .Binarywrite (ms.toarray ());} finally {g.dispose (); image.dispose ();}}}
If the above verification code generator page name is: checkcode.aspx, then "" in the login page to display the generated verification code picture: in the login page Use the following code to determine the verification code in the processing event of the login button:
Private void btnlogin_click (object sender, system.web.ui.ImageClickeventargs e) {if (request.cookies ["checkcode"] == null) {lblMessage.Text = "Your browser setting has been disabled, you must set The browser allows you to use the cookies option to use this system. "; LblMessage.visible = true; return;
IF (String.Compare (Request.Cookies]. Value, TxtCheckcode.text,
True)! = 0)
{
lblMessage.Text = "Verification code error, please enter the correct verification code."
LBlMessage.Visible = true;
Return;
}
/ *****
Other code ***** /
}