C# 產生驗證碼 詳解

來源:互聯網
上載者:User

using System;<br />using System.Data;<br />using System.Configuration;<br />using System.Collections;<br />using System.ComponentModel;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Web.UI.HtmlControls;<br />using System.Web.SessionState;<br />using System.Drawing;<br />using System.Drawing.Imaging;</p><p>namespace ShvnetAdmin<br />{<br /> public partial class CheckCode : System.Web.UI.Page<br /> {<br /> /**//// <summary><br /> /// 驗證碼長度<br /> /// </summary><br /> private int codeLen = 4;<br /> /**//// <summary><br /> /// 圖片寬度<br /> /// </summary><br /> private int imgWidth = 100;<br /> /**//// <summary><br /> /// 圖片高度<br /> /// </summary><br /> private int imgHeight = 35;<br /> /**//// <summary><br /> /// 字型大小<br /> /// </summary><br /> private int fontSize = 18;<br /> /**//// <summary><br /> /// 幹擾度(百分之幾)<br /> /// </summary><br /> private int fineness = 1;</p><p> protected void Page_Load(object sender, EventArgs e)<br /> {<br /> Response.Expires = 0;<br /> string validateCode = CreateValidateCode();<br /> Bitmap bitmap = new Bitmap(imgWidth, imgHeight);</p><p> //給映像設定幹擾<br /> DisturbBitmap(bitmap);</p><p> //繪製驗證碼映像<br /> DrawValidateCode(bitmap, validateCode);</p><p> //儲存驗證碼映像,等待輸出<br /> bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);<br /> }</p><p> /**//// <summary><br /> /// 隨機產生驗證碼,並儲存到SESSION中<br /> /// </summary><br /> /// <returns></returns><br /> private string CreateValidateCode()<br /> {<br /> string validateCode = "";<br /> Random random = new Random();<br /> string tmp = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";<br /> for (int i = 0; i < codeLen; i++)<br /> {<br /> int n = random.Next(tmp.Length);<br /> validateCode += tmp[n];<br /> }<br /> Session["ValidateCode"] = validateCode;<br /> return validateCode;<br /> }</p><p> /**//// <summary><br /> /// 為圖片設定幹擾點<br /> /// </summary><br /> /// <param name="bitmap"></param><br /> private void DisturbBitmap(Bitmap bitmap)<br /> {<br /> Random rand = new Random();<br /> int iRed = rand.Next(200, 255);<br /> int iGreen = rand.Next(200, 255);<br /> int iBlue = rand.Next(200, 255);<br /> int iAlpha = rand.Next(0, 100);<br /> // 通過隨機數產生<br /> Random random = new Random();<br /> for (int i = 0; i < bitmap.Width; i++)<br /> {<br /> for (int j = 0; j < bitmap.Height; j++)<br /> {<br /> if (random.Next(100) <= this.fineness)<br /> {<br /> //設定幹擾點<br /> iAlpha = rand.Next(0, 100);<br /> iRed = rand.Next(50, 150);<br /> iGreen = rand.Next(50, 150);<br /> iBlue = rand.Next(50, 150);<br /> bitmap.SetPixel(i, j, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> }<br /> else<br /> {<br /> //繪製背景色<br /> iAlpha = rand.Next(0, 100);<br /> iRed = rand.Next(240, 255);<br /> iGreen = rand.Next(200, 255);<br /> iBlue = rand.Next(200, 255);<br /> bitmap.SetPixel(i, j, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> }<br /> }<br /> }<br /> //畫邊框<br /> iAlpha = rand.Next(0, 100);<br /> iRed = rand.Next(0, 0);<br /> iGreen = rand.Next(0, 255);<br /> iBlue = rand.Next(0, 255);<br /> for (int i = 0; i < bitmap.Width; i++)<br /> {<br /> bitmap.SetPixel(i, 0, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(i, 1, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(i, bitmap.Height - 1, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(i, bitmap.Height - 2, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> }<br /> for (int i = 0; i < bitmap.Height; i++)<br /> {<br /> bitmap.SetPixel(0, i, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(1, i, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(bitmap.Width - 1, i, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> bitmap.SetPixel(bitmap.Width - 2, i, Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> }<br /> }</p><p> /**//// <summary><br /> /// 繪製驗證碼圖片<br /> /// </summary><br /> /// <param name="bitmap"></param><br /> /// <param name="validateCode"></param><br /> private void DrawValidateCode(Bitmap bitmap, string validateCode)<br /> {<br /> //隨機擷取顏色<br /> Random rand = new Random();<br /> int iRed = rand.Next(0, 50);<br /> int iGreen = rand.Next(0, 50);<br /> int iBlue = rand.Next(0, 50);<br /> int iAlpha = rand.Next(50, 100);<br /> char[] chr = validateCode.ToCharArray();<br /> for (int i = 0; i < codeLen; i++)<br /> {<br /> Font f = new Font(new FontFamily("Microsoft Sans Serif"), fontSize, FontStyle.Bold | FontStyle.Italic);<br /> Brush b = new SolidBrush(Color.FromArgb(iAlpha, iRed, iGreen, iBlue));<br /> Graphics g = Graphics.FromImage(bitmap);<br /> //設定各字元的漂浮範圍<br /> int iX, iY;<br /> iX = rand.Next(-5, 5);<br /> iY = rand.Next(-5, 5);<br /> //繪製驗證碼映像<br /> g.DrawString(chr[i].ToString(), f, b,<br /> (float)((bitmap.Width / codeLen) * (i) + iX),<br /> (float)(bitmap.Height / 2 - g.MeasureString(chr[i].ToString(), f).Height / 2 + iY));<br /> }<br /> }</p><p> }<br /> }</p><p>

調用頁面放個圖片,Src 指向這個驗證碼產生的頁面,Cs 代碼 判斷是否相同

String.Compare(Session["ValidateCode"].ToString(), this.txtcode.Text.Trim(), true) != 0

即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.