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
{/ ** /// /// Page Verification Code Program /// Use: Add HTML code in the page /// 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 / ** //// /// Set page is not cached /// summary> private void setpagenocache () {Response.Buffer = true; Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds (-1); Response.Expires = 0; Response.CacheControl = "no-cache"; Response.AppendHeader ( "Pragma", "No- Cache ");} / ** //// /// get a 4-bit random code /// summary> /// returns> private string getrandomcode () {Return GUID .Newguid (). Tostring (). Substring (0, 4);} / ** //// /// Random Take a font /// summary> /// returns >
Private font getFont () {int focinDex = _random.next (0, fontItems.Length); fontstyle fontstyle = getFontStyle (_random.next (0, 2)); return new font (FontItems [fontIndex], 12, fontstyle; / ** //// /// Take a font pattern /// summary> /// param> /// returns> Private fontstyle getFontStyle (int index) {switch (index) {copy 0: return fontstyle.bold; case 1: return fontstyle.italic; default: return fontstyle.regular;}} ////// // / Randomly take a brush /// /// returns> private brush getbrush () {int brushindex = _random.next (0, brushitems.length); _brushnameIndex = brushindex; return brushitems [ Brushindex];} #endre Gion // end of helper methods paint methods # region paint methods / ** //// /// Painting Event /// summary> private void onpaint () {bitmap objbitmap = null; graphics g = null ; try {objBitmap = new Bitmap (Width, Height); g = Graphics.FromImage (objBitmap); Paint_Background (g); Paint_Text (g); Paint_TextStain (objBitmap); Paint_Border (g); objBitmap.Save (Response.OutputStream, Imageformat.gif); response.contentType =
"image / gif";} catch {} finally {} (null! = objbitmap) objbitmap.dispose (); if (null! = g) g.dispose ();}} / ** //// /// Painting background color /// summary> /// param> private void point_background (graphics g) {g.clear (backcolor);} / ** /// / /// Painting Border /// summary> /// param> private void point_border (graphics g) {g.drawRectangle (Bordercolor, 0, 0, Width - 1, Height - 1);} / ** //// /// Painting text /// summary> /// param> private void point_text Graphics g) {g.drawstring (_code, getfont (), getbrush (), 3, 1);} / ** //// /// Painting text noise point /// summary> //// / param> Private void paint_textstain (bitmap b) {for (int N = 0; n <30; n ) {int x = _random.next (width); int y = _random.next (Height); B.Setpixel (x, y, Color.FromName (brushname [_brushnameindex]);}} #ENDREGON / / END of Paint Methods} // end of class} //
End of namespace
TRACKBACK: http://tb.blog.9cbs.net/trackback.aspx?postid=599093