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

C#和.NET提供了强大的功能和高效的开发环境。1)C#是一种现代、面向对象的编程语言,结合了C 的强大和Java的简洁性。2).NET框架是一个用于构建和运行应用程序的平台,支持多种编程语言。3)C#中的类和对象是面向对象编程的核心,类定义数据和行为,对象是类的实例。4).NET的垃圾回收机制自动管理内存,简化开发者的工作。5)C#和.NET提供了强大的文件操作功能,支持同步和异步编程。6)常见错误可以通过调试器、日志记录和异常处理来解决。7)性能优化和最佳实践包括使用StringBuild

.NETFramework是一个跨语言、跨平台的开发平台,提供一致的编程模型和强大的运行时环境。1)它由CLR和FCL组成,CLR管理内存和线程,FCL提供预构建功能。2)使用示例包括读取文件和LINQ查询。3)常见错误涉及未处理异常和内存泄漏,需使用调试工具解决。4)性能优化可通过异步编程和缓存实现,保持代码可读性和可维护性是关键。

C#.NET保持持久吸引力的原因包括其出色的性能、丰富的生态系统、强大的社区支持和跨平台开发能力。1)性能表现优异,适用于企业级应用和游戏开发;2).NET框架提供了广泛的类库和工具,支持多种开发领域;3)拥有活跃的开发者社区和丰富的学习资源;4).NETCore实现了跨平台开发,扩展了应用场景。

C#.NET中的设计模式包括Singleton模式和依赖注入。1.Singleton模式确保类只有一个实例,适用于需要全局访问点的场景,但需注意线程安全和滥用问题。2.依赖注入通过注入依赖提高代码灵活性和可测试性,常用于构造函数注入,但需避免过度使用导致复杂度增加。

C#.NET在现代世界中广泛应用于游戏开发、金融服务、物联网和云计算等领域。1)在游戏开发中,通过Unity引擎使用C#进行编程。2)金融服务领域,C#.NET用于开发高性能的交易系统和数据分析工具。3)物联网和云计算方面,C#.NET通过Azure服务提供支持,开发设备控制逻辑和数据处理。

C#.NET开发者社区提供了丰富的资源和支持,包括:1.微软的官方文档,2.社区论坛如StackOverflow和Reddit,3.GitHub上的开源项目,这些资源帮助开发者从基础学习到高级应用,提升编程技能。

C#.NET的优势包括:1)语言特性,如异步编程简化了开发;2)性能与可靠性,通过JIT编译和垃圾回收机制提升效率;3)跨平台支持,.NETCore扩展了应用场景;4)实际应用广泛,从Web到桌面和游戏开发都有出色表现。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具