Add a web project in .NET, add a .aspx page, name VerifyCode.aspx, go to its code editing state, will use the following code Copy & Paste, what comments or questions welcome ^ @ ^
Using
System;
Using
System.collections;
Using
System.componentmodel;
Using
System.data;
Using
System.drawing;
Using
System.Web;
Using
System.Web.SessionState;
Using
System.Web.ui;
Using
System.Web.ui.WebControls;
Using
System.Web.ui.htmlControls;
Using
System.drawing.Image;
Using
System.drawing.text;
Namespace
VerifyCode
{/ ** /// /// summary> public class verifycode: System.Web.ui.page {Private Members # Region Private Members Static String [] fontItems = new string [] {"Arial", "Helvetica", "Geneva", "Sans-Serif", "Verdana"}; static brush [] Brushitems = new brush [] {brushes.olivedrab, brushes.forestgreen, brushes.darkcyan, brushes.lightslate, brushes.royalblue, Brushes.SlateBlue, Brushes.DarkViolet, Brushes.MediumVioletRed, Brushes.IndianRed, Brushes.Firebrick, Brushes.Chocolate, Brushes.Peru, Brushes.Goldenrod}; static string [] BrushName = new string [] { "OliveDrab"
, "Forestgreen", "Darkcyan", "Royalblue", "SlateBlue", "Darkviolet", "MediumvioleTred", "Indianred", "Firebrick", "Chocolate", "Goldenrod" Private static color backcolor = color.white; private static pen bordercolor = pens.darkgray; private static int WIDT h = 52; private static int Height = 21; private Random _random; private string _code; private int _brushNameIndex; #endregion // end of Private Members Web Form Designer generated code # region Web Form Designer generated code override protected void OnInit (EventArgs e ) {/// codegen: this call is required by the ASP.NET Web form designer. // InitializationComponent (); base.onit (e);} / ** ////
/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// summary> private void InitializeComponent () {this.Load = new System.EventHandler (this.. Page_Load);} #endregion PageLoad Event # region PageLoad Event public void Page_Load (object sender, System.EventArgs e) {// // TODO: initialize // this._random = new Random (); this._code = GetRandomCode () ; // // Todo: Use session ["code"] save the verify /// session ["code"] = this._code; /// Todo: Output Image // this.SetPagenocacche (); this.onpaint );} #ENDREGON / / END OF PageLoad Event helper methods # region helper methods / ** ////
Private font getFont () {int focinDex = _random.next (0, fontItems.Length); fontstyle fontstyle = getFontStyle (_random.next (0, 2)); return new font (FontItems [fontIndex], 12, fontstyle; / ** ////
"image / gif";} catch {} finally {} (null! = objbitmap) objbitmap.dispose (); if (null! = g) g.dispose ();}} / ** ////
End of namespace
TRACKBACK: http://tb.blog.9cbs.net/trackback.aspx?postid=599093