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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具