C# · 12月 19, 2021

C#生成验证码

生成验证码的类:

using System; System.Collections.Generic; System.Drawing; 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,1)”>60),Color.FromArgb(128,1)”>0,1)”>12865,1)”>105,1)”>22570,1)”>130,1)”>18046,1)”>139,1)”>87184,1)”>134,1)”>11255,1)”>140,1)”>069,1)”>19191,1)”>25595,1)”>158,1)”>160147165,1)”>)}; 字体表 static string[] fonts = new string[] { “Arial”,VerdanaGeorgia黑体 }; 字体大小 int fontSize = 22; #endregion #region 生成验证码图片 生成验证码图片 static Bitmap CreateVerifyCodeBmp(out code) { int width = 120; int height = 40; Bitmap bmp = Bitmap(width,height); Graphics g = Graphics.FromImage(bmp); Random rnd = Random(); //背景色 g.FillRectangle(new SolidBrush(Color.White),new Rectangle(文字 StringBuilder sbCode = 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),1)”>new PointF((float)(i * width / 4.0),1)”>)); sbCode.Append(str); } code = sbCode.ToString(); 噪音线 10; i++int x1 = rnd.Next(bmp.Width); int x2 =int y1 = rnd.Next(bmp.Height); int y2 = rnd.Next(bmp.Height); Pen p = new Pen(GetColor(rnd),1)”>1); g.DrawLine(p,x1,y1,x2,y2); } 扭曲 bmp = TwistImage(bmp,1)”>true,1)”>3,rnd.NextDouble() * Math.PI * 2); g = Graphics.FromImage(bmp); 噪点 100; i++); g.DrawRectangle(p,1)”>1,1)”>); } 边框 g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(153,1)”>153))),width – )); return bmp; } #region 获取随机字符 获取随机字符 GetChar(Random rnd) { int n = rnd.Next(61); if (n <= 9return ((char)(48 + n)).ToString(); } else 3565 + n – 10)).ToString(); } else { 97 + n – 36)).ToString(); } } #region 获取随机字体 获取随机字体 static Font GetFont(Random rnd) { return new Font(fonts[rnd.Next(#region 获取随机颜色 获取随机颜色 Color GetColor(Random rnd) { return colors[rnd.Next(#region 正弦曲线Wave扭曲图片 <summary> 正弦曲线Wave扭曲图片(Edit By 51aspx.com) </summary> <param name=”srcBmp”>图片路径</param> <param name=”bXDir”>如果扭曲则选择为True<param name=”nMultValue”>波形的幅度倍数,越大扭曲的程度越高,一般为3<param name=”dPhase”>波形的起始相位,取值区间[0-2*PI)</param> static System.Drawing.Bitmap TwistImage(Bitmap srcBmp,1)”>bool bXDir,1)”>double dMultValue,1)”>double dPhase) { System.Drawing.Bitmap destBmp = Bitmap(srcBmp.Width,srcBmp.Height); 将位图背景填充为白色 System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp); graph.FillRectangle(new SolidBrush(System.Drawing.Color.White),destBmp.Width,destBmp.Height); graph.Dispose(); double dBaseAxisLen = bXDir ? (double)destBmp.Height : ()destBmp.Width; 0; i < destBmp.Width; i++int j = 0; j < destBmp.Height; j++) { double dx = ; dx = bXDir ? (Math.PI * 2 * (double)j) / dBaseAxisLen : (Math.PI * double)i) / dBaseAxisLen; dx += dPhase; double dy = Math.Sin(dx); 取得当前点的颜色 int nOldX = ; nOldX = bXDir ? i + (int)(dy * dMultValue) : i; nOldY = bXDir ? j : j + ( 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); } } } destBmp; } #endregion }}View Code

验证码页面Action:

public ActionResult VerifyCode(){ code; Bitmap bmp = VerifyCodeHelper.CreateVerifyCodeBmp(out code); Bitmap newbmp = new Bitmap(bmp,1)”>108,1)”>); HttpContext.Session[VerifyCode”] = code; Response.Clear(); Response.ContentType = image/bmp; newbmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Bmp); View();}View Code

说明:前台页面为空的cshtml页面,验证码的值放在Session中。

 

使用验证码的页面:

显示验证码的img:

<img id=”verifyCode” src=”” alt=”验证码” style=”vertical-align: middle;” />

页面加载完成后,显示验证码(注意,要加上时间戳,不然刷新页面时验证码不刷新):

$(function () { 刷新验证码 $(“#refreshVerifyCode”).click( () { refreshVerifyCode(); 刷新验证码 }); $(“#verifyCode”).click( }); refreshVerifyCode();});

刷新验证码:

refreshVerifyCode() { $(“#verifyCode”).attr(“src”,”VerifyCode?t=” + Date().valueOf());}

 

判断用户输入的文本是否与验证码相同的Action:

public ActionResult CheckVCode( vcode){ if (HttpContext.Session[“].ToString().ToLower() == vcode.ToLower()) { Dictionary<string,1)”>object> dic = new Dictionary<object>(); dic[ok”] = true Content(JsonConvert.SerializeObject(dic)); } { Dictionary<false Content(JsonConvert.SerializeObject(dic)); }}View Code