背景:

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. 【UI插件】简单的日历插件(下)—— 学习MVC思想

    前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...

  2. Mvc视图的那些事

    最近参与项目底层重写,在代码组织方式,类型使用上已经与之前有了很大的不同,这里总结一下视图的使用. 一.视图中命名空间的使用 视图命名空间的使用方式大致有三种:一,完全限定名,如 @System.Da ...

  3. 第一次react-native项目实践要点总结

    今天完成了我的第一个react-native项目的封包,当然其间各种环境各种坑,同时,成就感也是满满的.这里总结一下使用react-native的一些入门级重要点(不涉及环境).注意:阅读需要语法基础 ...

  4. 解决xcode升级之后安装的插件失效

    title: 解决xcode升级之后安装的插件失效date: 2015-08-23 11:07:53categories: 编辑工具 tags: xcode 我的博客:http://daycoding ...

  5. python-切片 迭代 生成器

    1 切片操作 >>> L ['aaa', 'bbb', 'ccc', 'ddd'] >>> L[0:3] ['aaa', 'bbb', 'ccc'] >> ...

  6. 2、软件设计师要阅读的书籍 - IT软件人员书籍系列文章

    软件设计师在项目组中的地位比软件工程师相对要高一些.但是他们所要阅读的书籍差别还是比较大的.同样的,软件设计师也要阅读比较多的书籍,以能够完成项目的任务为目的,同时还要提高自身在项目组中的竞争地位,而 ...

  7. MySQL备份还原——AutoMySQLBackup介绍

    AutoMySQLBackup是一个开源的MySQL备份脚本.可以说它是一个轻量级的备份方案,AutoMySQLBackup的安装.配置非常简单.方便.AutoMySQLBackup的sourcefo ...

  8. Failed to create AppDomain 'xxx'. Exception has been Failed to create AppDomain

    一服务器上的数据库全部被置于紧急模式(EMERGENCY),在错误日志里面能看到大量下面的错误 Failed to create AppDomain "YourSQLDba.dbo[runt ...

  9. PowerShell中的基础数据类型

    PowerShell是一个面向对象的语言,在申明变量的时候不强制要求申明数据类型,使用$开头来申明变量即可. 基本数据类型 PowerShell本身是基于.Net开发出来的,所以在.Net中的基本数据 ...

  10. SQL Server性能调优系列

    这是关于SQL Server调优系列文章,以下内容基本涵盖我们日常中所写的查询运算的分解以及调优内容项,皆为原创........ 第一个基础模块注重基础内容的掌握,共分7篇文章完成,内容涵盖一系列基础 ...