背景:

12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样。而多家抢票软件接近“报废”,引发不少网友不满的吐槽称“太抽象太艺术了”。

正题:

以前做项目有时候也会用到验证码,但基本都是静态的。这次也想凑凑12306的热闹。

闲言少续,切入正题,先上代码,实现方法:

 public void ShowCode()
{
//对象实例化
Validate GifValidate = new Validate(); #region 对验证码进行设置(不进行设置时,将以默认值生成)
//验证码位数,不小于4位
GifValidate.ValidateCodeCount = ;
//验证码字体型号(默认13)
GifValidate.ValidateCodeSize = ;
//验证码图片高度,高度越大,字符的上下偏移量就越明显
GifValidate.ImageHeight = ;
//验证码字符及线条颜色(需要参考颜色类)
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 = ;
protected string ValidateCode = "";
public string ValidateCodeFont = "Arial";
public float ValidateCodeSize = 13f; private void CreateImageBmp(out Bitmap ImageFrame)
{
char[] chArray = this.ValidateCode.ToCharArray(, 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 = ; i < this.TrueValidateCodeCount; i++)
{
int[] numArray = new int[] { (((int) (i * this.ValidateCodeSize)) + random.Next()) + , random.Next(maxValue) };
Point point = new Point(numArray[], numArray[]);
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();
encoder.SetRepeat();
for (int i = ; i < ; 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 = -;
Random random = new Random();
for (int i = ; i < this.ValidateCodeCount; i++)
{
if (index != -)
{
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[];
for (int i = ; i < ; i++)
{
pointArray[] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
pointArray[] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
graphics.DrawLine(pen, pointArray[], pointArray[]);
}
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 > )
{
this.TrueValidateCodeCount = value;
}
}
}
}

验证码效果:

16aspx下载

本地下载

由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)的更多相关文章

  1. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

  2. 16个ASP.NET MVC扩展点【附源码】

    转载于:http://www.cnblogs.com/wupeiqi/p/3570445.html 1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中! ...

  3. ASP.NET CORE 入门教程(附源码)

    ASP.NET CORE 入门教程 第一课 基本概念 基本概念 Asp.Net Core Mvc是.NET Core平台下的一种Web应用开发框架 符合Web应用特点 .NET Core跨平台解决方案 ...

  4. 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)

    在学习python开发框架pylons/pyramid的过程中,里面有个非常棒的页面性能监控功能,这样在开发过程中,你能清楚的知道当前页面的性能以及其它参数. 这里介绍一下如何给Asp.net MVC ...

  5. Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  6. ArcGIS紧凑型切片读取与应用2-webgis动态加载紧凑型切片(附源码)

    1.前言 上篇主要讲了一下紧凑型切片的的解析逻辑,这一篇主要讲一下使用openlayers动态加载紧凑型切片的web地图服务. 2.代码实现 上篇已经可以通过切片的x.y.z得对应的切片图片,现在使用 ...

  7. C#/ASP.NET MVC微信公众号接口开发之从零开发(四) 微信自定义菜单(附源码)

    C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...

  8. C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)

    C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码]

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码] 文件上传这东西说到底有时候很痛,原来的asp.net服务器 ...

随机推荐

  1. 程序员用HTML5制作的爱心树表白动画

    体验效果:http://keleyi.com/keleyi/phtml/html5/31.htm 推荐:http://hovertree.com/texiao/css3/18/ HTML代码如下: & ...

  2. CSS3文本溢出显示省略号

    CCS3属性之text-overflow:ellipsis;的用法和注意之处 语法: text-overflow:clip | ellipsis 默认值:clip 适用于:所有元素 clip: 当对象 ...

  3. Objective-C Runtime 运行时之四:Method Swizzling

    理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...

  4. Objective-C Runtime 运行时之一:类与对象

    Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一 ...

  5. Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较

    原文地址: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 话说在工作中第一次接触android 的Handler ...

  6. iOS 学习 - 17.Socket

    Socket 是应用层与 TCP / IP 协议通信的中间软件抽象层,它是一组接口   TCP:面向连接.传输可靠(保证数据正确性,保证数据顺序).用于传输大量数据(流模式).速度慢,建立连接需要开销 ...

  7. nginx File not found

    出现 nginx File not found 错误 首先判断这个文件是否 真的存在 如果文件确实存在 那么应该是 php-fpm 路径有问题了 解决办法 1查看 php-fpm 的配置文件 我的 文 ...

  8. 从AdventureWorks学习数据库建模——国际化

    前一篇博客我已经把各个实体分析了一遍,从分析中可以看到,这个公司是做本地采购,生产,然后通过网站和门店进行国际销售的.所以这里会涉及到一些国际化的问题.接下来就来分析一下有哪些国际化需要注意的问题和数 ...

  9. EasyUI 中点击取消按钮关闭Dialog(对话框窗口)

    <div id="dl1" class="easyui-dialog" title="窗口"style="width:400 ...

  10. .NET应用架构设计—表模块模式与事务脚本模式的代码编写

    阅读目录: 1.背景介绍 2.简单介绍表模块模式.事务脚本模式 3.正确的编写表模块模式.事务脚本模式的代码 4.总结 1.背景介绍 要想正确的设计系统架构就必须能正确的搞懂每个架构模式的用意,而不是 ...