理解C#生成验证码的过程
本文实例为大家介绍了生成验证码的详细过程,供大家参考,具体内容如下
生成验证码的类:
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:
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;" />
页面加载完成后,显示验证码(注意,要加上时间戳,不然刷新页面时验证码不刷新):
$(function () { //刷新验证码 $("#refreshVerifyCode").click(function () { refreshVerifyCode(); //刷新验证码 }); $("#verifyCode").click(function () { refreshVerifyCode(); //刷新验证码 }); refreshVerifyCode(); });
刷新验证码:
//刷新验证码 function refreshVerifyCode() { $("#verifyCode").attr("src", "VerifyCode?t=" + new Date().valueOf()); }
判断用户输入的文本是否与验证码相同的Action:
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#生成验证码的方法有所帮助。
栏 目:C#教程
下一篇:C# 利用ICSharpCode.SharpZipLib实现在线压缩和解压缩
本文标题:理解C#生成验证码的过程
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6609.html
您可能感兴趣的文章
- 01-10C#获取动态生成的CheckBox值
- 01-10关于nancy中的身份验证
- 01-10基于C#实现简单离线注册码生成与验证
- 01-10C#身份证号码验证是否正确
- 01-10c#实现识别图片上的验证码数字
- 01-1012306奇葩验证码引发思考之C#实现验证码程序
- 01-10基于C#实现12306的动态验证码变成静态验证码的方法
- 01-10基于C#生成条形码操作知识汇总附源码下载
- 01-10理解C#中参数的值和引用以及传递结构和类引用的区别
- 01-10理解C#编程中的静态类和静态成员以及密封类
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery