您好,登錄后才能下訂單哦!
本篇內容介紹了“如何編寫ASP.NET實現動態GIF驗證碼”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
實現方法:
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實現動態GIF驗證碼”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。