12306动态验证码启发之ASP.NET实现动态GIF验证码(附源码)

 更新时间:2022年05月03日 12:26:32   投稿:lijiao  
这篇文章主要介绍了受到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;
      //定义验证码中所有的字符(","分离),似乎暂时不支持中文
      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");
    }

调用主要方法:

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[] { ',' });
      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;
        }
      }
    }
  }

验证码效果:       -----下载源码-----

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

相关文章

  • asp.net 分页sql语句(结合aspnetpager)

    asp.net 分页sql语句(结合aspnetpager)

    一直用的是存储过程分页,小项目一般不写存储过程,就需要直接写分页sql语句。
    2009-01-01
  • 实现文件和文件夹的复制的方法

    实现文件和文件夹的复制的方法

    本文主要分享了实现文件和文件夹的复制的示例代码,具有一定的参考价值,下面跟着小编一起来看下吧
    2016-12-12
  • ASP.NET Core根据环境变量支持多个 appsettings.json配置文件

    ASP.NET Core根据环境变量支持多个 appsettings.json配置文件

    这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • .Net Core依赖注入IOC和DI介绍

    .Net Core依赖注入IOC和DI介绍

    这篇文章介绍了.Net Core依赖注入IOC和DI,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • .NET Core 实现一个自定义日志记录器

    .NET Core 实现一个自定义日志记录器

    在应用程序中,日志记录是一个至关重要的功能,不仅有助于调试和监控应用程序,还能帮助我们了解应用程序的运行状态,所以本文就将使用.NET Core 实现一个自定义日志记录器,需要的可以参考下
    2024-12-12
  • ASP.NET MVC3关于生成纯静态后如何不再走路由直接访问静态页面

    ASP.NET MVC3关于生成纯静态后如何不再走路由直接访问静态页面

    高访问量类型的电子商务网站,需要将一些不是经常变化的页面生成静态页面,然后普通用户就可以直接访问这些静态页面而不用再访问需要连接数据库的动态页面。那么ASP.NET MVC3中如何做到这一点呢
    2011-12-12
  • .NET逻辑分层架构总结

    .NET逻辑分层架构总结

    本人将从另一个角度来解析.NET分层架构的真正奥秘。分层,一些技术功底比较薄弱的程序员听到分层就会联想到三层架构(BLL,DAL之类的),其实不是,分层是一个很大的技术框架思想,三层架构只不过是对普通的信息系统来说,将信息的流转通过三层来分解,
    2015-06-06
  • asp.net下检测SQL注入式攻击代码

    asp.net下检测SQL注入式攻击代码

    防网站被攻击代码
    2009-09-09
  • ajaxpro.dll 控件实现异步刷新页面

    ajaxpro.dll 控件实现异步刷新页面

    用ajaxpro.dll控件实现异步刷新页面的代码,需要的朋友可以参考下。
    2009-11-11
  • 发布一个基于TokyoTyrant的C#客户端开源项目

    发布一个基于TokyoTyrant的C#客户端开源项目

    目前在网上关于TokyoCabinet(以下简称TC)和TokyoTyrant(以下简称TT)的资料已相对丰富了,但在.NET平台上的客户端软件却相对匮乏,因为做Discuz!NT企业版的关系,两个月前开始接触TC和TT,开始写相关的客户端代码。
    2010-07-07

最新评论