C # Verification Code

Source: Internet
Author: User

usingSystem;usingSystem.Collections;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Xml.Linq;usingSystem.Drawing;usingSystem.Web.SessionState;namespacemt{/// <summary>    ///Summary description of $codebehindclassname $/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)]  Public classVercode:ihttphandler, IRequiresSessionState {PrivateRandom Randomseed =NewRandom ();  Public voidProcessRequest (HttpContext context) {//generate random number iIlLoO0            stringStrword ="23456789QWERTYUPASDFGHKXCVBNM"; stringNumstr =NULL;  for(inti =0; I <5; i++) {Numstr+ = Strword[randomseed.next (0, Strword.length)]; }            //Save the Verification code to the sessionContext. session["Vcode"] =Numstr.tolower ();        Createimages (context, NUMSTR); }        #regionGenerate a Captcha picturePrivate voidCreateimages (HttpContext context,stringCheckcode) {            intIwidth = (int) (Checkcode.length * -); System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap (iwidth, A); Graphics g=graphics.fromimage (image);            G.clear (Color.White); //Define ColorColor[] C ={color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color.Purple}; //Defining Fonts            string[] Font = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","Song Body" }; Random Rand=NewRandom (); //random output noise.             for(inti =0; I < -; i++)            {                intx =Rand. Next (image.                Width); inty =Rand. Next (image.                Height); G.drawrectangle (NewPen (Color.lightgray,0), X, Y,1,1); }            //output CAPTCHA characters for different fonts and colors             for(inti =0; i < checkcode.length; i++)            {                intCIndex = Rand. Next (7); intFindex = Rand. Next (5); Font F=NewSystem.Drawing.Font (Font[findex],Ten, System.Drawing.FontStyle.Bold); Brush b=NewSystem.Drawing.SolidBrush (C[cindex]); intII =4; if((i +1) %2==0) {II=2; } g.drawstring (Checkcode.substring (i,1), F, B,2+ (I * A), ii); }            //Draw a borderG.drawrectangle (NewPen (colortranslator.fromhtml ("#CCCCCC"),0),0,0, image. Width-1, image. Height-1); //Output to BrowserSystem.IO.MemoryStream ms =NewSystem.IO.MemoryStream (); Image.            Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); Context.            Response.clearcontent (); //response.clearcontent ();Context. Response.ContentType ="Image/gif"; Context. Response.BinaryWrite (Ms.            ToArray ());            G.dispose (); Image.        Dispose (); }        #endregion         Public BOOLisreusable {Get            {                return false; }        }    }}

C # Verification Code

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.