由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)
背景:
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;
}
}
}
}
验证码效果:

由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)的更多相关文章
- 17+个ASP.NET MVC扩展点【附源码】
1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig. 在自定义的Http ...
- 16个ASP.NET MVC扩展点【附源码】
转载于:http://www.cnblogs.com/wupeiqi/p/3570445.html 1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中! ...
- ASP.NET CORE 入门教程(附源码)
ASP.NET CORE 入门教程 第一课 基本概念 基本概念 Asp.Net Core Mvc是.NET Core平台下的一种Web应用开发框架 符合Web应用特点 .NET Core跨平台解决方案 ...
- 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)
在学习python开发框架pylons/pyramid的过程中,里面有个非常棒的页面性能监控功能,这样在开发过程中,你能清楚的知道当前页面的性能以及其它参数. 这里介绍一下如何给Asp.net MVC ...
- Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)
上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...
- ArcGIS紧凑型切片读取与应用2-webgis动态加载紧凑型切片(附源码)
1.前言 上篇主要讲了一下紧凑型切片的的解析逻辑,这一篇主要讲一下使用openlayers动态加载紧凑型切片的web地图服务. 2.代码实现 上篇已经可以通过切片的x.y.z得对应的切片图片,现在使用 ...
- C#/ASP.NET MVC微信公众号接口开发之从零开发(四) 微信自定义菜单(附源码)
C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...
- C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)
C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码]
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码] 文件上传这东西说到底有时候很痛,原来的asp.net服务器 ...
随机推荐
- Group-buy项目总结
这是我做的第一个移动端项目,和传统PC端网站不同的是,做移动端的网站要适配各种尺寸的设备. 在默认情况下,移动设备上的viewport都是要大于浏览器可视区域的,这是因为考虑到移动设备的分辨率相对于桌 ...
- Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
前言 啦啦啦~大家好,又见面啦~ 本篇博文讲和大家一起完成一个需要注册.登录的备忘录的,一起学习 SharedPreferences 的基本使用,学习 Android 中常见的文件操作方法,复习 An ...
- iOS--UICollectionView(滚动视图)入门
UICollectionView @interface UICollectionView : UIScrollView UICollectionView 和UICollectionViewCon ...
- iOS --NSAttributedString
字符属性可以应用于 attributed string 的文本中. 文/iOS_成才录(简书作者) 原文链接:http://www.jianshu.com/p/03a741246737 著作权归作者所 ...
- python之局部变量引用赋值前的结果
通过正则表达式,实现加减 昨晚在做计算器的时候,被一个BUG搞懵比了.现在再看看,发现我好小白啊~~ #++- num = input("please input:") sa = ...
- jquery数组删除指定元素的方法:grep()
jquery数组删除指定元素的方法:grep() 金刚 数组 jquery javascript 元素 遇到的问题 今天遇到一个问题,删除数组中的一个指定元素,并返回新的数组. 我定义的js数组是这样 ...
- Android应用开发基础之十二:版本控制
为什么需要版本控制? 场景1: 你的代码正常工作 你改了其中的几行代码 程序出了问题 你把代码改回来 程序还是不能正常工作——为什么? 场景2: 你的程序昨天还能正常运行 昨天晚上你修改了很多内容,做 ...
- Html--表单练习
<!--文档定义一定要带上,因为浏览器在解析的时候先按照文档定义的格式解析, 如果没有就按照浏览器默认的格式解析,可能会出问题.--> <html> & ...
- 监控mysql各种选项
安装mysql之后,需要对mysql服务进行监控. nagios开源自带的check_mysql 对 mysql 的slave 机监控倒是不错.但是对数据库主机监控就略显不足了. 使用一个监控 ...
- oracle一些基本语句
--添加一个字段alter table 表名 add(列类型);--修改字段数据类型alter table 表名 modify(列类型);--删除一个字段alter table 表名 drop col ...