由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服务器 ...
随机推荐
- jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
上一篇记录了BaiduTemplate模板引擎使用示例附源码,在此基础上对使用方法进行了封装 自定义插件jajaxrefresh.js 代码如下: //闭包限定命名空间 (function ($) { ...
- 弹出页面遮罩层,以及web端和移动端阻止遮罩层的滑动。
最近项目遇到了遮罩层的一些问题,总结一下: 弹出遮罩层 遮罩层弹出有非常多的方法,这里只写出本人用的代码,使用jq操作dom的方法进行实现的. <style>.box{position:a ...
- jsonp 演示实例 —— 基于node
序 同源策略是浏览器处于安全考虑,为通信设置了"相同的域.相同的端口.相同的协议"这一限制.这让我们的ajax请求存在跨域无权限访问的问题. 同时我们发现script标签引入脚本的 ...
- ABAP使用OLE2对象创建EXCEL文件
厌倦了总是下载一模一样的EXCEL文档?没有颜色,边框,有效性验证.... 让我们看看怎样用OLE2对象来创造可爱的EXCEL工作表吧!(效果如下) 首先你需要知道微软EXCEL中的不同部分的名称,每 ...
- [Android]Google 开源的 Android 排版库:FlexboxLayout
最近Google开源了一个项目叫「FlexboxLayout」. 1.什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方 ...
- Volley框架设置sessionid
(偷懒,写简略点) 自定义一个Request类 public class MyRequest extends Request<JSONObject> 存储上一次连接的sessionid ...
- 【Android】实现XML解析的几种技术
本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...
- iOS---A valid provisioning profile for this executable was not found
把Target中的Code Signing Identity也设置成iPhone Develop就ok了,这样一切都说的通了,唯一不合理的就是在Project切换Code Signing Identi ...
- CocoaPods的使用及安装
本文转自:http://www.jianshu.com/p/6e5c0f78200a 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发i ...
- SQLite浅析
对于iOS工程师有一道常考的面试题,即iOS数据存储的方式 标答如下: Plist(NSArray\NSDictionary) Preference (偏好设置\NSUserDefaults) NSC ...