12306 dynamic verification code inspired by ASP.net implementation of dynamic GIF verification code (with source code) _ Practical Skills

Source: Internet
Author: User
Tags httpcontext

12306 website launched the "Color dynamic verification Code mechanism", the new version of the verification code not only often appear characters stacked pressure, but also constantly jitter, many people's Congress called "See not clear," said the verification code, Picasso's abstract painting Mody! Iron Total customer Service said: In order to be able to buy tickets can only be so. and a number of ticket software close to "scrap", causing many netizens dissatisfied with the spit said "too abstract too artistic."
Used to do projects are sometimes used to verify the code, but the basic is static, this time also want to gather together 12306 of the lively. Gossip less continue, cut to the chase, first on the code.

Implementation method:

 public void ShowCode ()
    {
      //object instantiation
      Validate gifvalidate = new Validate ();

      #region The validation code is set (the default value will be generated when not set)
      //Verify code digits, not less than 4 bits
      gifvalidate.validatecodecount = 4;
      CAPTCHA font Model (default)
      gifvalidate.validatecodesize =;
      Verification code picture height, the higher the height, the character of the upper and lower offset is more obvious
      gifvalidate.imageheight =;
      Verify code character and line color (need reference color Class)
      Gifvalidate.drawcolor = System.Drawing.Color.BlueViolet;
      Authenticode font (need to fill in Server installed fonts)
      Gifvalidate.validatecodefont = "Arial";
      Verify that the code character is anti-aliasing
      gifvalidate.fonttextrenderinghint = false;
      Define all the characters in the Verification code ("," detach), it seems to temporarily do not support the 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");
    

Invoke the primary 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 (imagef
      Rame); 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 {
      Truevalidatecodecount; set {if (Value > 4) {this.
        Truevalidatecodecount = value;
 }
      }
    }
  }

Verification Code Effect:-----Download Source Code-----

The above is to realize the whole process of asp.net, but also with the source code, hope to help you better understand the ASP.net verification code generation method.

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.