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(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,)}; /// <summary>
/// 字体表
/// </summary>
private static string[] fonts = new string[] {
"Arial",
"Verdana",
"Georgia",
"黑体" }; /// <summary>
/// 字体大小
/// </summary>
private static int fontSize = ;
#endregion #region 生成验证码图片
/// <summary>
/// 生成验证码图片
/// </summary>
public static Bitmap CreateVerifyCodeBmp(out string code)
{
int width = ;
int height = ;
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
Random rnd = new Random(); //背景色
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(, , width, height)); //文字
StringBuilder sbCode = new StringBuilder();
for (int i = ; i < ; 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), ));
sbCode.Append(str);
}
code = sbCode.ToString(); //噪音线
for (int i = ; i < ; 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), );
g.DrawLine(p, x1, y1, x2, y2);
} //扭曲
bmp = TwistImage(bmp, true, , rnd.NextDouble() * Math.PI * );
g = Graphics.FromImage(bmp); //噪点
for (int i = ; i < ; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height); Pen p = new Pen(GetColor(rnd), );
g.DrawRectangle(p, x1, y1, , );
} //边框
g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(, , ))), new Rectangle(, , width - , height - )); return bmp;
}
#endregion #region 获取随机字符
/// <summary>
/// 获取随机字符
/// </summary>
private static string GetChar(Random rnd)
{
int n = rnd.Next(, );
if (n <= )
{
return ((char)( + n)).ToString();
}
else if (n <= )
{
return ((char)( + n - )).ToString();
}
else
{
return ((char)( + n - )).ToString();
}
}
#endregion #region 获取随机字体
/// <summary>
/// 获取随机字体
/// </summary>
private static Font GetFont(Random rnd)
{
return new Font(fonts[rnd.Next(, fonts.Length)], fontSize, FontStyle.Bold);
}
#endregion #region 获取随机颜色
/// <summary>
/// 获取随机颜色
/// </summary>
private static Color GetColor(Random rnd)
{
return colors[rnd.Next(, 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), , , destBmp.Width, destBmp.Height);
graph.Dispose(); double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width; for (int i = ; i < destBmp.Width; i++)
{
for (int j = ; j < destBmp.Height; j++)
{
double dx = ;
dx = bXDir ? (Math.PI * * (double)j) / dBaseAxisLen : (Math.PI * * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx); // 取得当前点的颜色
int nOldX = , nOldY = ;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue); System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= && nOldX < destBmp.Width
&& nOldY >= && 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, , );
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));
}
}
上一篇:如何决定DCOM是否可用


下一篇:第八篇 Replication:合并复制-How it works