12306 Dynamic Verification code-inspired ASP. NET implementation Dynamic GIF verification code

Source: Internet
Author: User
12306 website launched "Color Dynamic verification Code mechanism", the new version of the verification code not only often appear character stack pressure, but also constantly jitter, many people's Congress call "see unclear", said "the verification code, is the abstract painting of Picasso!" Iron Total customer Service said: In order to be able to buy the normal only this way. and a number of ticket-grabbing software close to the "scrap", triggering a number of users dissatisfied with the spit groove called "too abstract too artistic."
Used to do the project sometimes will use the verification code, but the basic is static, this time also want to gather together 12306 lively. Gossip less continued, cut to the chase, first on the code.

Implementation method:

public void ShowCode ()   {     //object instantiation     Validate gifvalidate = new Validate ();      #region The verification code is set (when not set, will be generated by default)     //Verify the number of code bits, not less than 4 bit     gifvalidate.validatecodecount = 4;     CAPTCHA font Model (default)     gifvalidate.validatecodesize =;     Verification code picture height, the higher the height of the character of the upper and lower offset is more obvious     gifvalidate.imageheight =;     Verification code character and line color (need reference color Class)     Gifvalidate.drawcolor = System.Drawing.Color.BlueViolet;     Captcha font (need to fill in Server installed fonts)     Gifvalidate.validatecodefont = "Arial";     Verify that the code character is anti-aliasing     gifvalidate.fonttextrenderinghint = false;     Defines all the characters in the Captcha ("," detach), which seems to temporarily not support Chinese     Gifvalidate.allchar = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q , r,s,t,u,w,x,y,z ";     #endregion      //Output image (session name)     gifvalidate.outputvalidate ("GetCode");   }

Call the Main method:

public class Validate {public string Allchar = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z    ";    Public Color drawcolor = Color.blueviolet;    public bool Fonttextrenderinghint = FALSE;    public int imageheight = 0x17;    Private byte Truevalidatecodecount = 4;    protected string validatecode = "";    public string validatecodefont = "Arial";     public float validatecodesize = 13f; private void Createimagebmp (out Bitmap imageframe) {char[] Charray = this. Validatecode.tochararray (0, this.      Validatecodecount); int width = (int) ((this. Truevalidatecodecount * this.      Validatecodesize) * 1.3) + 4.0); ImageFrame = new Bitmap (width, this.      ImageHeight);      Graphics graphics = graphics.fromimage (imageframe); Graphics.      Clear (Color.White); Font font = new Font (this. Validatecodefont, this.      Validatecodesize, FontStyle.Bold); Brush brush = new SolidBrush (this.      Drawcolor); int maxValue = (int) Math.max ((float) (this. ImageHeight- This.      validatecodesize)-3f), (float) 2f);      Random random = new random (); for (int i = 0; I < this. Truevalidatecodecount; i++) {int[] Numarray = new int[] {((int) (I * this. validatecodesize) + random. Next (1)) + 3, random.        Next (MaxValue)};        Point point = new Point (Numarray[0], numarray[1]); if (this. Fonttextrenderinghint) {graphics.        TextRenderingHint = Textrenderinghint.singlebitperpixel; } else {graphics.        TextRenderingHint = Textrenderinghint.antialias; } graphics. DrawString (Charray[i].      ToString (), Font, brush, (PointF) point); } graphics.    Dispose ();      } private void Createimagegif () {Animatedgifencoder encoder = new Animatedgifencoder ();      MemoryStream stream = new MemoryStream (); Encoder.      Start (); Encoder.      Setdelay (5); Encoder.      Setrepeat (0);        for (int i = 0; i < i++) {Bitmap Bitmap; This.  Createimagebmp (out bitmap);      This.        Disposeimagebmp (ref bitmap); Bitmap.        Save (stream, imageformat.png); Encoder.        Addframe (Image.fromstream (stream));      stream = new MemoryStream (); } encoder.      OutPut (ref stream);      HttpContext.Current.Response.ClearContent ();      HttpContext.Current.Response.ContentType = "Image/gif"; HttpContext.Current.Response.BinaryWrite (stream.      ToArray ()); Stream.      Close (); Stream.    Dispose (); private void Createvalidate () {this.      Validatecode = ""; String[] Strarray = this.      Allchar.split (new char[] {', '});      int index =-1;      Random random = new random (); for (int i = 0; I < this. Validatecodecount;        i++) {if (Index! =-1) {random = new random ((i * index) * ((int) DateTime.Now.Ticks)); } int num3 = random.        Next (0x23); if (index = = num3) {this.        Createvalidate ();        } index = num3; This. Validatecode = this. Validatecode + Strarray[index]; } if (this. Validatecode.length > this. Truevalidatecodecount) {this. Validatecode = this. Validatecode.remove (this.      Truevalidatecodecount); }} private void Disposeimagebmp (ref Bitmap ImageFrame) {Graphics graphics = Graphics.fromimage (imageframe      ); Pen pen = new Pen (this.      Drawcolor, 1f);      Random random = new random ();      point[] Pointarray = new point[2]; for (int i = 0; i <, i++) {pointarray[0] = new Point (random. Next (Imageframe.width), random.        Next (imageframe.height)); POINTARRAY[1] = new Point (random. Next (Imageframe.width), random.        Next (imageframe.height)); Graphics.      DrawLine (pen, pointarray[0], pointarray[1]); } graphics.    Dispose (); } public void Outputvalidate (string validatecodesession) {this.      Createvalidate (); This.      Createimagegif (); Httpcontext.current.session[validatecodesession] = this.    Validatecode;    } public byte Validatecodecount {get  {return this.      Truevalidatecodecount; } set {if (Value > 4) {this.        Truevalidatecodecount = value; }      }    }  }

Above is the implementation of the whole process of ASP, but also with the source code, hoping to help everyone better understand the method of generating ASP.

Related Article

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.