PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了12306动态验证码启发之ASP.NET实现动态GIF验证码(附源码)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样。而多家抢票软件接近“报废”,引发不少网友不满的吐槽称“太抽象太艺术了”。
以前做项目有时候也会用到验证码,但基本都是静态的,这次也想凑凑12306的热闹。闲言少续,切入正题,先上代码

实现方法

 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;
      //定义验证码中所有的字符(","分离),似乎暂时不支持@L_607_9@
      GifValidate.AllChar = "1,2,3,4,5,6,7,8,9,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");
    }

调用主要方法

public class Validate
  {
    public String AllChar = "1,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[] { ',' });
      int index = -1;
      Random random = new Random();
      for (int i = 0; i < this.ValidateCodeCount; i++)
      {
        if (index != -1)
        {
          random = new Random((i * indeX) * ((int) datetiR_478_11845@e.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;
        }
      }
    }
  }

验证码效果:@H_944_22@@H_944_22@12306动态验证码启发之ASP.NET实现动态GIF验证码(附源码)

       -----下载源码-----

以上就是实现ASP.NET的全部过程,还附有源码,希望可以帮到大家更好地了解ASP.NET验证码的生成方法

大佬总结

以上是大佬教程为你收集整理的12306动态验证码启发之ASP.NET实现动态GIF验证码(附源码)全部内容,希望文章能够帮你解决12306动态验证码启发之ASP.NET实现动态GIF验证码(附源码)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:验证码