12306動態驗證碼啟發之ASP.NET實作動態GIF驗證碼
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中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

C 語言中符號的使用方法涵蓋算術、賦值、條件、邏輯、位運算符等。算術運算符用於基本數學運算,賦值運算符用於賦值和加減乘除賦值,條件運算符用於根據條件執行不同操作,邏輯運算符用於邏輯操作,位運算符用於位級操作,特殊常量用於表示空指針、文件結束標記和非數字值。

C語言中通過轉義序列處理特殊字符,如:\n表示換行符。 \t表示製表符。使用轉義序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要轉義兩次。不同平台和編譯器可能有不同的轉義序列,請查閱文檔。

在 C 語言中,char 類型在字符串中用於:1. 存儲單個字符;2. 使用數組表示字符串並以 null 終止符結束;3. 通過字符串操作函數進行操作;4. 從鍵盤讀取或輸出字符串。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

在 C 語言中,char 類型轉換可以通過:強制類型轉換:使用強制類型轉換符將一種類型的數據直接轉換為另一種類型。自動類型轉換:當一種類型的數據可以容納另一種類型的值時,編譯器自動進行轉換。

在 C 語言中,char 和 wchar_t 的主要區別在於字符編碼:char 使用 ASCII 或擴展 ASCII,wchar_t 使用 Unicode;char 佔用 1-2 個字節,wchar_t 佔用 2-4 個字節;char 適用於英語文本,wchar_t 適用於多語言文本;char 廣泛支持,wchar_t 依賴於編譯器和操作系統是否支持 Unicode;char 的字符範圍受限,wchar_t 的字符範圍更大,並使用專門的函數進行算術運算。

char 數組在 C 語言中存儲字符序列,聲明為 char array_name[size]。訪問元素通過下標運算符,元素以空終止符 '\0' 結尾,用於表示字符串終點。 C 語言提供多種字符串操作函數,如 strlen()、strcpy()、strcat() 和 strcmp()。

C語言中沒有內置求和函數,需自行編寫。可通過遍歷數組並累加元素實現求和:循環版本:使用for循環和數組長度計算求和。指針版本:使用指針指向數組元素,通過自增指針遍歷高效求和。動態分配數組版本:動態分配數組並自行管理內存,確保釋放已分配內存以防止內存洩漏。
