ASP. NET implemented dynamic GIF Verification Code (with source code) that comes with the idea of 12306 dynamic verification code)

Source: Internet
Author: User

Background: The "color dynamic verification code mechanism" was launched on the 12306 website. The new Verification Code not only frequently experienced overlapping characters, but also kept shaking. Many people call the "that verification code, is it Picasso's abstract painting!" Iron's customer service said: in order to be able to purchase tickets normally, this is the only way. As many ticket snatching software approaches "decommission", many netizens complained that "too abstract and too artistic ". Subject: verification codes are sometimes used in previous projects, but they are basically static. This time I also want to gather together the excitement of 12306. In this case, let's start with the main question. Run the code first. Implementation Method: copy the public void ShowCode () {// instantiate the object Validate GifValidate = new Validate (); # region sets the verification code (generated by default when not set) // number of digits of the verification code, no less than four GifValidate. validateCodeCount = 4; // Verification Code font model (default 13) GifValidate. validateCodeSize = 13; // The height of the Verification Code image. The higher the height, the more obvious the upper and lower offsets of characters are. imageHeight = 23; // The Verification Code character and line color (see the color class for details. drawColor = System. drawing. color. bluevilet; // the font of the Verification Code (the font of the Server installation is required ). E. validateCodeFont = "Arial"; // whether the characters in the verification code are de-duplicated. fontTextRenderingHint = false; // defines all characters ("," separated) in the verification code. It seems that Chinese GifValidate is not supported currently. 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");} main method of copying Code call: copy the code public class Validate {public string AllChar = ", 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. bluevilet; 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. V AlidateCodeCount); 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 (f Loat) (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 <10; 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. clo Se (); 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 <15; 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 ;}}} copy the code

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.