ASP.NET inspired by 12306 dynamic verification code implements dynamic GIF verification code

高洛峰
Release: 2017-01-13 15:28:50
Original
1623 people have browsed it

12306 website launched a "color dynamic verification code mechanism". Not only did the new version of the verification code often have overlapping characters, but it also kept shaking. Many people shouted that they couldn't see clearly and said, "That verification code is an abstract painting by Picasso." What!" The customer service of China Railway Corporation said: This is the only way to be able to purchase tickets normally. Many ticket-grabbing software are on the verge of being scrapped, causing dissatisfaction from many netizens who complained that they were “too abstract and artistic.”
In the past, verification codes were sometimes used in projects, but they were basically static. This time I wanted to join in the fun of 12306. Without further ado, let’s get to the point and start with the code.

Implementation method:

public void ShowCode()
   {
     //对象实例化
     Validate GifValidate = new Validate();
 
     #region 对验证码进行设置(不进行设置时,将以默认值生成)
     //验证码位数,不小于4位
     GifValidate.ValidateCodeCount = 4;
     //验证码字体型号(默认13)
     GifValidate.ValidateCodeSize = 13;
     //验证码图片高度,高度越大,字符的上下偏移量就越明显
     GifValidate.ImageHeight = 23;
     //验证码字符及线条颜色(需要参考颜色类)
     GifValidate.DrawColor = System.Drawing.Color.BlueViolet;
     //验证码字体(需要填写服务器安装的字体)
     GifValidate.ValidateCodeFont = "Arial";
     //验证码字符是否消除锯齿
     GifValidate.FontTextRenderingHint = false;
     //定义验证码中所有的字符(","分离),似乎暂时不支持中文
     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
 
     //输出图像(Session名称)
     GifValidate.OutPutValidate("GetCode");
   }
Copy after login

Calling 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 < 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.Close();
      stream.Dispose();
    }
 
    private void CreateValidate()
    {
      this.ValidateCode = "";
      string[] strArray = this.AllChar.Split(new char[] { &#39;,&#39; });
      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 after login

The above is the entire process of implementing ASP.NET, and the source code is also attached. I hope it can help everyone better Learn how to generate ASP.NET verification codes.

For more articles related to ASP.NET implementing dynamic GIF verification code inspired by 12306 dynamic verification code, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!