Page verification code program (C #)

xiaoxiao2021-03-17  177

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 /// 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 /// 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 /// 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 /// /// private string getrandomcode () {Return GUID .Newguid (). Tostring (). Substring (0, 4);} / ** //// /// Random Take a font /// ///

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 /// /// /// 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 /// 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 /// 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 /// /// private void point_background (graphics g) {g.clear (backcolor);} / ** /// / /// Painting Border /// /// private void point_border (graphics g) {g.drawRectangle (Bordercolor, 0, 0, Width - 1, Height - 1);} / ** //// /// Painting text /// /// private void point_text Graphics g) {g.drawstring (_code, getfont (), getbrush (), 3, 1);} / ** //// /// Painting text noise point /// //// / 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

转载请注明原文地址:https://www.9cbs.com/read-129631.html

New Post(0)