国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

由12306動(dòng)態(tài)驗(yàn)證碼想到的ASP.NET實(shí)現(xiàn)動(dòng)態(tài)GIF驗(yàn)證碼(附源碼)

2019-11-15 02:25:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

由12306動(dòng)態(tài)驗(yàn)證碼想到的asp.net實(shí)現(xiàn)動(dòng)態(tài)GIF驗(yàn)證碼(附源碼)

背景:

12306網(wǎng)站推出“彩色動(dòng)態(tài)驗(yàn)證碼機(jī)制”,新版驗(yàn)證碼不但經(jīng)常出現(xiàn)字符疊壓,還不停抖動(dòng),不少人大呼“看不清”,稱(chēng)“那個(gè)驗(yàn)證碼,是畢加索的抽象畫(huà)么!”鐵總客服則表示:為了能正常購(gòu)票只能這樣。而多家搶票軟件接近“報(bào)廢”,引發(fā)不少網(wǎng)友不滿(mǎn)的吐槽稱(chēng)“太抽象太藝術(shù)了”。

正題:

以前做項(xiàng)目有時(shí)候也會(huì)用到驗(yàn)證碼,但基本都是靜態(tài)的。這次也想湊湊12306的熱鬧。

閑言少續(xù),切入正題,先上代碼,實(shí)現(xiàn)方法:

 public void ShowCode()        {            //對(duì)象實(shí)例化            Validate GifValidate = new Validate();            #region 對(duì)驗(yàn)證碼進(jìn)行設(shè)置(不進(jìn)行設(shè)置時(shí),將以默認(rèn)值生成)            //驗(yàn)證碼位數(shù),不小于4位            GifValidate.ValidateCodeCount = 4;            //驗(yàn)證碼字體型號(hào)(默認(rèn)13)            GifValidate.ValidateCodeSize = 13;            //驗(yàn)證碼圖片高度,高度越大,字符的上下偏移量就越明顯            GifValidate.ImageHeight = 23;            //驗(yàn)證碼字符及線條顏色(需要參考顏色類(lèi))            GifValidate.DrawColor = System.Drawing.Color.BlueViolet;            //驗(yàn)證碼字體(需要填寫(xiě)服務(wù)器安裝的字體)            GifValidate.ValidateCodeFont = "Arial";            //驗(yàn)證碼字符是否消除鋸齒            GifValidate.FontTextRenderingHint = false;            //定義驗(yàn)證碼中所有的字符(","分離),似乎暫時(shí)不支持中文            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名稱(chēng))            GifValidate.OutPutValidate("GetCode");        }

調(diào)用主要方法:

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;                }            }        }    }

驗(yàn)證碼效果:

16aspx下載

本地下載


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 保德县| 平舆县| 凤山县| 万安县| 宝坻区| 陆川县| 金昌市| 宜宾县| 蛟河市| 新民市| 洮南市| 博兴县| 原平市| 栖霞市| 麦盖提县| 西藏| 西平县| 乌鲁木齐市| 石嘴山市| 文成县| 九龙城区| 望江县| 砚山县| 佛坪县| 南宫市| 保德县| 女性| 阳高县| 卢龙县| 北海市| 莲花县| 灯塔市| 维西| 福鼎市| 西藏| 巴林左旗| 泰州市| 南投县| 慈溪市| 定边县| 蓝山县|