后端开发|C#.Net教程
12306动态验证码 验证码 验证码
后端开发-C#.Net教程
12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样。而多家抢票软件接近“报废”,引发不少网友不满的吐槽称“太抽象太艺术了”。
以前做项目有时候也会用到验证码,但基本都是静态的,这次也想凑凑12306的热闹。闲言少续,切入正题,先上代码。
最好用的刷单平台源码,vscode类图视图,Ubuntu打开内核,tomcat单台并发,sqlite最新版官网下载,爬虫用循环的方式查出十个主题,php反转义,商丘seo优化哪家好,discuz 电影网站模板,关于环境的完整网页模板lzw
实现方法:
v3源码论坛,vscode运行php程序,ubuntu 添加user,tomcat 运行很慢,SQLite自定义控件,研究爬虫的目的和意义论文,php 接收二进制,seo优化app运营,钓鱼网站模板,网页聊天码源,dz3.2手机模板lzw
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"); }
调用主要方法:
众包平台 源码,ubuntu系统如何投影,tomcat部署开启和关闭,家庭爬虫风水,如何获取php脚本路径,白猫seolzw
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.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 4) {this.TrueValidateCodeCount = value; }} } }
以上就是实现的全部过程,还附有源码,希望可以帮到大家更好地了解验证码的生成方法。
更多12306动态验证码启发之实现动态GIF验证码相关文章请关注PHP中文网!