12306动态验证码启发之ASP.NET实现动态GIF验证码

高洛峰
发布: 2017-01-13 15:28:50
原创
2213人浏览过

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

实现方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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");

   }

登录后复制

调用主要方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

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验证码的生成方法。

更多12306动态验证码启发之ASP.NET实现动态GIF验证码相关文章请关注PHP中文网!

铁路12306
铁路12306

铁路12306是一款由中铁程科技有限责任公司官方发行推出的购票订票软件。这款软件能够帮助用户随时随地查询火车高铁的信息,不管是想要购票还是退票改签都可以在这里进行操作,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号