程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 理解C#生成驗證碼的過程

理解C#生成驗證碼的過程

編輯:關於C語言

本文實例為大家介紹了生成驗證碼的詳細過程,供大家參考,具體內容如下

生成驗證碼的類:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 using System; using System.Collections.Generic; using System.Drawing; using System.Text; namespace Controllers.Core.Util { /// <summary> /// 驗證碼 /// </summary> public class VerifyCodeHelper : AdminBaseController { #region 變量 /// <summary> /// 顏色表 /// </summary> private static Color[] colors = new Color[]{ Color.FromArgb(220,20,60), Color.FromArgb(128,0,128), Color.FromArgb(65,105,225), Color.FromArgb(70,130,180), Color.FromArgb(46,139,87), Color.FromArgb(184,134,11), Color.FromArgb(255,140,0), Color.FromArgb(139,69,19), Color.FromArgb(0,191,255), Color.FromArgb(95,158,160), Color.FromArgb(255,20,147), Color.FromArgb(255,165,0)}; /// <summary> /// 字體表 /// </summary> private static string[] fonts = new string[] { "Arial", "Verdana", "Georgia", "黑體" }; /// <summary> /// 字體大小 /// </summary> private static int fontSize = 22; #endregion #region 生成驗證碼圖片 /// <summary> /// 生成驗證碼圖片 /// </summary> public static Bitmap CreateVerifyCodeBmp(out string code) { int width = 120; int height = 40; Bitmap bmp = new Bitmap(width, height); Graphics g = Graphics.FromImage(bmp); Random rnd = new Random(); //背景色 g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, width, height)); //文字 StringBuilder sbCode = new StringBuilder(); for (int i = 0; i < 4; i++) { string str = GetChar(rnd); Font font = GetFont(rnd); Color color = GetColor(rnd); g.DrawString(str, font, new SolidBrush(color), new PointF((float)(i * width / 4.0), 0)); sbCode.Append(str); } code = sbCode.ToString(); //噪音線 for (int i = 0; i < 10; i++) { int x1 = rnd.Next(bmp.Width); int x2 = rnd.Next(bmp.Width); int y1 = rnd.Next(bmp.Height); int y2 = rnd.Next(bmp.Height); Pen p = new Pen(GetColor(rnd), 1); g.DrawLine(p, x1, y1, x2, y2); } //扭曲 bmp = TwistImage(bmp, true, 3, rnd.NextDouble() * Math.PI * 2); g = Graphics.FromImage(bmp); //噪點 for (int i = 0; i < 100; i++) { int x1 = rnd.Next(bmp.Width); int y1 = rnd.Next(bmp.Height); Pen p = new Pen(GetColor(rnd), 1); g.DrawRectangle(p, x1, y1, 1, 1); } //邊框 g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(153, 153, 153))), new Rectangle(0, 0, width - 1, height - 1)); return bmp; } #endregion #region 獲取隨機字符 /// <summary> /// 獲取隨機字符 /// </summary> private static string GetChar(Random rnd) { int n = rnd.Next(0, 61); if (n <= 9) { return ((char)(48 + n)).ToString(); } else if (n <= 35) { return ((char)(65 + n - 10)).ToString(); } else { return ((char)(97 + n - 36)).ToString(); } } #endregion #region 獲取隨機字體 /// <summary> /// 獲取隨機字體 /// </summary> private static Font GetFont(Random rnd) { return new Font(fonts[rnd.Next(0, fonts.Length)], fontSize, FontStyle.Bold); } #endregion #region 獲取隨機顏色 /// <summary> /// 獲取隨機顏色 /// </summary> private static Color GetColor(Random rnd) { return colors[rnd.Next(0, colors.Length)]; } #endregion #region 正弦曲線Wave扭曲圖片 /// <summary> /// 正弦曲線Wave扭曲圖片(Edit By 51ASPx.com) /// </summary> /// <param name="srcBmp">圖片路徑</param> /// <param name="bXDir">如果扭曲則選擇為True</param> /// <param name="nMultValue">波形的幅度倍數,越大扭曲的程度越高,一般為3</param> /// <param name="dPhase">波形的起始相位,取值區間[0-2*PI)</param> private static 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 ? (Math.PI * 2 * (double)j) / DBaseAxisLen : (Math.PI * 2 * (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 } }

驗證碼頁面Action:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 public ActionResult VerifyCode() { string code; Bitmap bmp = VerifyCodeHelper.CreateVerifyCodeBmp(out code); Bitmap newbmp = new Bitmap(bmp, 108, 36); HttpContext.Session["VerifyCode"] = code; Response.Clear(); Response.ContentType = "image/bmp"; newbmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp); return VIEw(); }

說明:前台頁面為空的csHtml頁面,驗證碼的值放在Session中。

使用驗證碼的頁面:

顯示驗證碼的img:

<img id="verifyCode" src="" alt="驗證碼" style="vertical-align: middle;" />
頁面加載完成後,顯示驗證碼(注意,要加上時間戳,不然刷新頁面時驗證碼不刷新):

? 1 2 3 4 5 6 7 8 9 10 $(function () { //刷新驗證碼 $("#refreshVerifyCode").click(function () { refreshVerifyCode(); //刷新驗證碼 }); $("#verifyCode").click(function () { refreshVerifyCode(); //刷新驗證碼 }); refreshVerifyCode(); });

刷新驗證碼:

? 1 2 3 4 5 //刷新驗證碼 function refreshVerifyCode() { $("#verifyCode").attr("src", "VerifyCode?t=" + new Date().valueOf()); }

判斷用戶輸入的文本是否與驗證碼相同的Action:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ActionResult CheckVCode(string vcode) { if (HttpContext.Session["VerifyCode"].ToString().ToLower() == vcode.ToLower()) { Dictionary<string, object> dic = new Dictionary<string, object>(); dic["ok"] = true; return Content(JSonConvert.SerializeObject(dic)); } else { Dictionary<string, object> dic = new Dictionary<string, object>(); dic["ok"] = false; return Content(JSonConvert.SerializeObject(dic)); } }

以上就是本文的全部內容,希望對大家學習C#生成驗證碼的方法有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved