图片验证码生成
2015-12-17 16:44
399 查看
/// <summary>
/// 自动生成图片验证码
/// </summary>
/// <param name="checkCode">返回的图片验证码</param>
/// <param name="width">显示到客户端的图片宽度</param>
/// <param name="height">显示到客户端的图片高度</param>
/// <param name="checkCodeLength">验证码长度</param>
/// <returns>流Stream返回</returns>
public static byte[] CreateImgVidCode(ref string checkCode, int width = 124, int height = 40, int checkCodeLength = 5)
{
var rnd = new Random();
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'k', 'm', 'p', 'q', 'w', 'r', 's', 't', 'v', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X',
'Y' };
checkCode = string.Empty;
var checkCodeFontSize = 16;
for (int i = 0; i < checkCodeLength; i++)
{
checkCode += character[rnd.Next(character.Length)];
}
using (Bitmap image = new Bitmap(width, height))
using (Graphics graphic = Graphics.FromImage(image))
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
StringFormat stringFormat = new StringFormat();
stringFormat.LineAlignment = StringAlignment.Center;
stringFormat.Alignment = StringAlignment.Center;
Rectangle lbRect = new Rectangle((width - 16 * checkCodeLength) / 6, (height - checkCodeFontSize) / 6, width, height);
Font font = new Font("Arial", checkCodeFontSize, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(checkCode, font, brush, lbRect, stringFormat);
int x = 0;
int y = 0;
for (int i = 0; i < 100; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
using (var ms = new System.IO.MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
}
===========================
=======html
<div class="n-input-box list-input2">
<label class="verification">
<i></i>
<input name="ImgVidCode" id="ImgVidCode" type="text" placeholder="请输入图形验证码" />
</label>
<img src="@Url.Action("CreateImgVidCode", "MemMember", new { vt = "R" })" width="124" height="40" alt="" onclick="this.src = this.src + '?'" style="cursor:pointer">
@*<p class="error" id="ImgCodeErrMsg"></p>*@
@Html.Raw("<p class='error' id='ImgCodeErrMsg'>" + System.Web.HttpUtility.HtmlDecode(Html.ValidationMessage("ImgCodeErrMsg", new { @class = "error" }).ToString()) + "</p>")
</div>
====controller
/// 功能:图片验证码(5分钟内有效)
/// </summary>
/// <param name="vt">类型:L:登录;R:注册;S:设置交易密码;F:找回密码;B:绑定新手机</param>
/// <returns></returns>
public FileResult CreateImgVidCode(string vt = "L")
{
//验证码
var code = string.Empty;
//压力测试
//var code = "666666";
//对应的键
var cacheKey = string.Empty;
vt = vt.ToUpper().Replace("?", "");
switch (vt)
{
default:
case "L":
cacheKey = base.LoginImgVidCodeCacheKey;
break;
case "R":
cacheKey = base.RegisterImgVidCodeCacheKey;
break;
case "F":
cacheKey = base.FindPwdImgVidCodeCacheKey;
break;
case "S":
cacheKey = base.SetPayPassWordMsgVidCodeCacheKey;
break;
case "B":
cacheKey = base.BindingNewPhoneCacheKey;
break;
}
//取6位验证码或者5位
var s = vt.Equals("L") || vt.Equals("F") || vt.Equals("S") || vt.Equals("B") ? CodeHelpers.CreateImgVidCode(ref code, checkCodeLength: 6) : CodeHelpers.CreateImgVidCode(ref code);
//压力测试
//var s = ClientHelper.CreateImgVidCode(code);
//缓存验证码
//HK.CM.Web.Utility.Common.CacheHelpers.set
HKSJ.Common.DataCache.SetCache(cacheKey, code, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
return File(s, "image/Jpeg");
}
/// 自动生成图片验证码
/// </summary>
/// <param name="checkCode">返回的图片验证码</param>
/// <param name="width">显示到客户端的图片宽度</param>
/// <param name="height">显示到客户端的图片高度</param>
/// <param name="checkCodeLength">验证码长度</param>
/// <returns>流Stream返回</returns>
public static byte[] CreateImgVidCode(ref string checkCode, int width = 124, int height = 40, int checkCodeLength = 5)
{
var rnd = new Random();
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'k', 'm', 'p', 'q', 'w', 'r', 's', 't', 'v', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X',
'Y' };
checkCode = string.Empty;
var checkCodeFontSize = 16;
for (int i = 0; i < checkCodeLength; i++)
{
checkCode += character[rnd.Next(character.Length)];
}
using (Bitmap image = new Bitmap(width, height))
using (Graphics graphic = Graphics.FromImage(image))
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
StringFormat stringFormat = new StringFormat();
stringFormat.LineAlignment = StringAlignment.Center;
stringFormat.Alignment = StringAlignment.Center;
Rectangle lbRect = new Rectangle((width - 16 * checkCodeLength) / 6, (height - checkCodeFontSize) / 6, width, height);
Font font = new Font("Arial", checkCodeFontSize, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(checkCode, font, brush, lbRect, stringFormat);
int x = 0;
int y = 0;
for (int i = 0; i < 100; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
using (var ms = new System.IO.MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
}
===========================
=======html
<div class="n-input-box list-input2">
<label class="verification">
<i></i>
<input name="ImgVidCode" id="ImgVidCode" type="text" placeholder="请输入图形验证码" />
</label>
<img src="@Url.Action("CreateImgVidCode", "MemMember", new { vt = "R" })" width="124" height="40" alt="" onclick="this.src = this.src + '?'" style="cursor:pointer">
@*<p class="error" id="ImgCodeErrMsg"></p>*@
@Html.Raw("<p class='error' id='ImgCodeErrMsg'>" + System.Web.HttpUtility.HtmlDecode(Html.ValidationMessage("ImgCodeErrMsg", new { @class = "error" }).ToString()) + "</p>")
</div>
====controller
/// 功能:图片验证码(5分钟内有效)
/// </summary>
/// <param name="vt">类型:L:登录;R:注册;S:设置交易密码;F:找回密码;B:绑定新手机</param>
/// <returns></returns>
public FileResult CreateImgVidCode(string vt = "L")
{
//验证码
var code = string.Empty;
//压力测试
//var code = "666666";
//对应的键
var cacheKey = string.Empty;
vt = vt.ToUpper().Replace("?", "");
switch (vt)
{
default:
case "L":
cacheKey = base.LoginImgVidCodeCacheKey;
break;
case "R":
cacheKey = base.RegisterImgVidCodeCacheKey;
break;
case "F":
cacheKey = base.FindPwdImgVidCodeCacheKey;
break;
case "S":
cacheKey = base.SetPayPassWordMsgVidCodeCacheKey;
break;
case "B":
cacheKey = base.BindingNewPhoneCacheKey;
break;
}
//取6位验证码或者5位
var s = vt.Equals("L") || vt.Equals("F") || vt.Equals("S") || vt.Equals("B") ? CodeHelpers.CreateImgVidCode(ref code, checkCodeLength: 6) : CodeHelpers.CreateImgVidCode(ref code);
//压力测试
//var s = ClientHelper.CreateImgVidCode(code);
//缓存验证码
//HK.CM.Web.Utility.Common.CacheHelpers.set
HKSJ.Common.DataCache.SetCache(cacheKey, code, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
return File(s, "image/Jpeg");
}
相关文章推荐
- HTML5知识介绍
- 转:Python模块——struct(字节流,组包拆包实现)
- win10系统Powershell重置应用失效的解决办法
- TCP/IP、Http、Socket的区别
- 用viewpager+fragment实现仿华为自定义铃声
- uinavigationcontroller手势返回
- 【ALearning】第三章 Android基本常见控件
- 关于线上与线下性能测试结果的差异
- 论Python的单例模式和装饰器
- php+nginx+mysql+ubuntu环境搭建
- 剑指offer第三十六题【平衡二叉树】c++实现
- Hadoop Combiner类 简单测试
- Python学习二:Django Web入门
- tableView下拉框的实现
- android 自定义键盘 KeyboardView的key 文字颜色发虚模糊
- Oracle授权 同义词
- Log4j 日志工具demo以及配置文件实例
- Yii 1.1代码生成器gii(booster gii)模板修改
- div与span区别及用法
- python 提取sqlit数据