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

首頁 > 編程 > C# > 正文

12306奇葩驗證碼引發思考之C#實現驗證碼程序

2020-01-24 01:20:35
字體:
來源:轉載
供稿:網友

近日鐵路訂票網“12306”又出現多道另類考題,竟要訂票者在8個圖案中“點擊圖中所有美男子”、“請點擊下圖中所有的非智能眼鏡”、“請點擊下圖中所有的博斯普魯斯海峽”,網友吐槽:比高考題還難,到底是什么樣子的,先跟大家分享一下幾個例子:

哈哈,是有點奇葩的驗證碼,怪不得有人會說“媽媽我已經找不到回家”,這讓分秒必爭的春運網上搶票者瞬間傻眼,九成網友已經被打敗……

正巧小編最近也在研究驗證碼,參考了許多網上案例,整理了一篇文章特分享給大家。

驗證碼的一般編寫思路為:
       1.定義驗證碼字符長度;
       2.根據長度隨機生成驗證碼字符串;
       3.將驗證碼字符串轉換成圖片形式,并在圖片中生成隨機噪聲點和聲線(對驗證碼進行模糊識別處理);
       4.顯示結果。

 /// /// 生成隨機驗證碼 /// /// 驗證碼長度 /// public string CreateIdentifyingCode(int CodeLen) { if (CodeLen < 1)  return String.Empty; int num; string checkcode = String.Empty; Random random = new Random(); for (int index = 0; index < CodeLen; index++) {  num = random.Next();  if (num % 2 == 0)  checkcode += (char)('0' + (char)(num % 10));  else  checkcode += (char)('A' + (char)(num % 26)); } return checkcode; }------------------------------------------------------------------------------------------------- /// /// 生成驗證碼圖片 /// /// ///  private void CreateCheckCodeImage(string checkcode) {  if (checkcode == null || checkcode.Trim() == String.Empty)  return;  //創建圖片大小  System.Drawing.Bitmap image = new  System.Drawing.Bitmap((int)Math.Ceiling(checkcode.Length*12.5),22);  //創建畫板  Graphics graphic = Graphics.FromImage(image);   try  {  Random random = new Random();  graphic.Clear(Color.White);  int x1 = 0, y1 = 0, x2 = 0, y2 = 0;  //畫圖片背景噪聲線  for (int index = 0; index < 25; index++)  {   x1 = random.Next(image.Width);   y1 = random.Next(image.Height);   x2 = random.Next(image.Width);   y2 = random.Next(image.Height);   graphic.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);  }  Font font = new System.Drawing.Font("Helvetica", 12, (FontStyle.Bold |FontStyle.Italic));  LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue,Color.DarkBlue,1.2f,true);  graphic.DrawString(checkcode,font,brush,2,2);   int x = 0;  int y = 0;  // 畫圖片的前景噪聲點  for (int index = 0; index < 100; index++)  {   x = random.Next(image.Width);   y = random.Next(image.Height);   image.SetPixel(x,y,Color.FromArgb(random.Next()));  }  //畫圖片的邊框線  graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);  //網頁響應  System.IO.MemoryStream ms = new System.IO.MemoryStream();  image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);  Response.ClearContent();  Response.ContentType = "image/Gif";  Response.BinaryWrite(ms.ToArray());  }  finally  {  graphic.Dispose();  image.Dispose();  } }

以上所生成的為簡單的驗證碼。接下來我從其他的博客中學習了其他形式的效果。

/* 圖片畫線特殊效果:貝塞爾曲線 */  Graphics graph = Graphics.FromImage(image);  graph.Clear(Color.WhiteSmoke);  Point[] myArray ={     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50)),     new Point(random.Next(150),random.Next(50))    };  Pen myPen = new Pen(Color.Blue, 1);  GraphicsPath myPath = new GraphicsPath();  myPath.AddBeziers(myArray);  graph.DrawPath(myPen, myPath);

驗證碼字符顏色變換效果:實現該效果,我們首先來定義一個顏色集合,然后通過for循環使其隨機改變字體顏色則可。

 #region 定義顏色數組 Color[] colors = { Color.Blue, Color.Green, Color.Red, Color.Gold, Color.Black, Color.Chocolate, Color.Orange, Color.Purple }; public Color[] Colors {  get { return colors; }  set { colors = value; } } #endregion Brush brush; int colornum; for(int i=0; i {  colornum = random.Next(Colors.Length - 1);  brush = new System.Drawing.SolidBrush(Colors[cindex]);   //利用DrawString函數進行顏色填充就可以了。 }

 同樣的原理我們也可以定義一個字體的數組來進行驗證碼字體切換。代碼和顏色的類似,這里就不加以累贅。
接下來看看如何使得驗證碼的字體進行扭曲。

private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; /// /// 波形濾鏡效果函數 /// /// /// /// /// /// public System.Drawing.Bitmap TwistImage(Bitmap srcbmp, double dmultvalue, double dphase) {  System.Drawing.Bitmap destbmp = new Bitmap(srcbmp.Width,srcbmp.Height);  System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destbmp);  //填充背景圖為白色  graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destbmp.Width, destbmp.Height);  graph.Dispose();  double dbaselen = (double)destbmp.Width;  for (int i = 0; i < destbmp.Width; i++)  {  for (int j = 0; j < destbmp.Height; j++)  {   double dx = 0;   dx = (PI2 * (double)j) / dbaselen;   dx += dphase;    double dy = Math.Sin(dx);    int noldx = 0, noldy = 0;   noldx = i + (int)(dy * dmultvalue);   noldy = j + (int)(dy * dmultvalue);   System.Drawing.Color color = srcbmp.GetPixel(i, j);   if (noldx >= 0 && noldx < destbmp.Width && noldy >= 0 && noldy < destbmp.Height)   destbmp.SetPixel(noldx, noldy, color);  }  }  return destbmp; }

上面代碼是參考了這段代碼進行的學習<C#.net 好用的驗證碼代碼 漢字-變色-扭曲-波動 >,代碼如下

using System;using System.Data;using System.Configuration;using System.Collections;using System.Drawing;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class study_CheckCode2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string code = CreateVerifyCode();  //取隨機碼 CreateImageOnPage(code, this.Context); // 輸出圖片 Response.Cookies.Add(new HttpCookie("CheckCode", code.ToUpper()));// 使用Cookies取驗證碼的值 } #region 驗證碼長度(默認4個驗證碼的長度) int length = 4; public int Length { get { return length; } set { length = value; } } #endregion #region 驗證碼字體大小(為了顯示扭曲效果,默認40像素,可以自行修改) int fontSize = 40; public int FontSize { get { return fontSize; } set { fontSize = value; } } #endregion #region 邊框補(默認1像素) int padding = 2; public int Padding { get { return padding; } set { padding = value; } } #endregion #region 是否輸出燥點(默認不輸出) bool chaos = true; public bool Chaos { get { return chaos; } set { chaos = value; } } #endregion #region 輸出燥點的顏色(默認灰色) Color chaosColor = Color.LightGray; public Color ChaosColor { get { return chaosColor; } set { chaosColor = value; } } #endregion #region 自定義背景色(默認白色) Color backgroundColor = Color.White; public Color BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; } } #endregion #region 自定義隨機顏色數組 Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; public Color[] Colors { get { return colors; } set { colors = value; } } #endregion #region 自定義字體數組 string[] fonts = { "Arial", "Georgia" }; public string[] Fonts { get { return fonts; } set { fonts = value; } } #endregion #region 自定義隨機碼字符串序列(使用逗號分隔) //string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; string codeSerial = "阿,保,持,的,法,規,和,東,三,省,問,我,惹,你,誒,沒,改,變,揍,屁,股,吧"; public string CodeSerial { get { return codeSerial; } set { codeSerial = value; } } #endregion #region 產生波形濾鏡效果 private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; /// <summary> /// 正弦曲線Wave扭曲圖片(Edit By 51aspx.com) /// </summary> /// <param name="srcBmp">圖片路徑</param> /// <param name="bXDir">如果扭曲則選擇為True</param> /// <param name="dMultValue">波形的幅度倍數,越大扭曲的程度越高,一般為3</param> /// <param name="dPhase">波形的起始相位,取值區間[0-2*PI)</param> /// <returns></returns> public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) { System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height); // 將位圖背景填充為白色 System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp); graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height); graph.Dispose(); double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width; for (int i = 0; i < destBmp.Width; i++) {  for (int j = 0; j < destBmp.Height; j++)  {  double dx = 0;  dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;  dx += dPhase;  double dy = Math.Sin(dx);  // 取得當前點的顏色  int nOldX = 0, nOldY = 0;  nOldX = bXDir ? i + (int)(dy * dMultValue) : i;  nOldY = bXDir ? j : j + (int)(dy * dMultValue);  System.Drawing.Color color = srcBmp.GetPixel(i, j);  if (nOldX >= 0 && nOldX < destBmp.Width   && nOldY >= 0 && nOldY < destBmp.Height)  {   destBmp.SetPixel(nOldX, nOldY, color);  }  } } return destBmp; } #endregion #region 生成校驗碼圖片 public Bitmap CreateImageCode(string code) { int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = (int)(code.Length * fWidth) + 30 + Padding * 2; int imageHeight = fSize * 2 + Padding; System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight); Graphics g = Graphics.FromImage(image); g.Clear(BackgroundColor); Random rand = new Random(); //給背景添加隨機生成的燥點 if (this.Chaos) {  Pen pen = new Pen(ChaosColor, 0);  int c = Length * 10;  for (int i = 0; i < c; i++)  {  int x = rand.Next(image.Width);  int y = rand.Next(image.Height);  g.DrawRectangle(pen, x, y, 1, 1);  } } int left = 0, top = 0, top1 = 1, top2 = 1; int n1 = (imageHeight - FontSize - Padding * 2); int n2 = n1 / 4; top1 = n2; top2 = n2 * 2; Font f; Brush b; int cindex, findex; //隨機字體和顏色的驗證碼字符 for (int i = 0; i < code.Length; i++) {  cindex = rand.Next(Colors.Length - 1);  findex = rand.Next(Fonts.Length - 1);  f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);  b = new System.Drawing.SolidBrush(Colors[cindex]);  if (i % 2 == 1)  {  top = top2;  }  else  {  top = top1;  }  left = i * fWidth;  g.DrawString(code.Substring(i, 1), f, b, left, top); } //畫一個邊框 邊框顏色為Color.Gainsboro g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1); g.Dispose(); //產生波形(Add By 51aspx.com) image = TwistImage(image, true, 8, 4); return image; } #endregion #region 將創建好的圖片輸出到頁面 public void CreateImageOnPage(string code, HttpContext context) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); Bitmap image = this.CreateImageCode(code); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/Jpeg"; context.Response.BinaryWrite(ms.GetBuffer()); ms.Close(); ms = null; image.Dispose(); image = null; } #endregion #region 生成隨機字符碼 public string CreateVerifyCode(int codeLen) { if (codeLen == 0) {  codeLen = Length; } string[] arr = CodeSerial.Split(','); string code = ""; int randValue = -1; Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < codeLen; i++) {  randValue = rand.Next(0, arr.Length - 1);  code += arr[randValue]; } return code; } public string CreateVerifyCode() { return CreateVerifyCode(0); } #endregion}

一年一度的搶票熱潮又開始了,希望大家都能順利買到回家的火車篇,回家過年,突然感覺有點年味了,一年又一年,時間都去哪了,小小的感慨一下……

言歸正傳,這就是為大家分享的C#驗證碼程序,和12306驗證碼差多了,不過也是小編的學習收獲吧!大家也可以結合下面這兩篇文章進行學習:

《12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)》

《12306驗證碼破解思路分享》

希望本文所述對大家學習驗證碼技術有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北海市| 南漳县| 和龙市| 抚顺县| 景宁| 两当县| 奇台县| 利辛县| 于都县| 泸溪县| 同德县| 陵水| 新沂市| 舞钢市| 东乡县| 洛浦县| 古蔺县| 汕头市| 宣汉县| 大厂| 友谊县| 襄汾县| 阳江市| 大邑县| 石台县| 梁平县| 安丘市| 英吉沙县| 恩施市| 昆明市| 绥江县| 桐城市| 信宜市| 金门县| 金寨县| 从江县| 白水县| 涪陵区| 方正县| 西乡县| 武义县|