.net 生成随机验证码图片
(注:此方法更适用于.net前后端不分离项目)
第一步:创建一个验证码的控制器ValidateCodeController
第二步:在控制器粘贴随机生成四位数验证码图片的代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace MovieHomeWork.Areas.admin.Controllers
{
public class ValidateCodeController : Controller
{
// GET: admin/ValidateCode
public ActionResult GetCode()
{
///获取随机码
var code = getRandomStr(4);
Session["Code"] = code;
///生成图片对象
var bitmap = getImage(code, "ch");
///图片对象转为字节数组
byte[] data = bitmapToByte(bitmap);
///输出文件
return File(data, "image/gif");
}
/// <summary>
/// 获取随机码
/// </summary>
/// <param name="count">随机码个数</param>
/// <returns>随机码</returns>
private string getRandomStr(int count)
{
if (count == 0) throw new ArgumentException("错误的字符长度!", "strCount");
StringBuilder sb = new StringBuilder();
var strs = "0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w s y z " +
"A B C D E F G H I J K L M N O P Q R S T U V W S Y Z";
var strArray = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Random random = new Random();
for (int i = 0; i < count; i++)
{
sb.Append(strArray[random.Next(strArray.Length)]);
}
return sb.ToString();
}
/// <summary>
/// 根据随机码生成bitmap对象
/// </summary>
/// <param name="code">随机码</param>
/// <param name="type">en,ch</param>
/// <returns>bitmap对象</returns>
private Bitmap getImage(string code, string type = "en")
{
if (string.IsNullOrWhiteSpace(code)) throw new ArgumentException("随机码为空!", "type");
int step = 0;
if (type == "ch")
{
step = 5;
}
///英文字符个数
int charLength = code.Length;
///bitmap的宽度(像素)
int iWidth = charLength * (13 + step);
///bitmap的高度(像素)
int iHeight = 22;
///创建图片
Bitmap image = new Bitmap(iWidth, iHeight);
///绘画图片对象
Graphics g = Graphics.FromImage(image);
///清除背景并填充白色
g.Clear(Color.White);
///画干扰点
Random random = new Random();
for (int i = 0; i < 50; i++)
{
int x1 = random.Next(iWidth);
int x2 = random.Next(iWidth);
int y1 = random.Next(iHeight);
int y2 = random.Next(iHeight);
g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);
}
///字体颜色
Color[] colors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange };
///字体设置
string[] fontFamilies = new string[] { "黑体", "Bahnschrift SemiLight", "Comic Sans MS", "MV Boli", "Segoe Print" };
for (int i = 0; i < charLength; i++)
{
int colorIndex = random.Next(colors.Length);
int fontIndex = random.Next(fontFamilies.Length);
///随机字体
Font font = new Font(fontFamilies[fontIndex], 10, FontStyle.Bold);
///用来填充多边形的内部对象(填充字体)
Brush brush = new SolidBrush(colors[colorIndex]);
///开始绘画字符
g.DrawString(code.Substring(i, 1), font, brush, i * (12 + step), 4);
}
///绘画矩形边框
//g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, image.Width - 1, image.Height - 1);
return image;
}
/// <summary>
/// bitmap对象转换为字节数组
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private byte[] bitmapToByte(Bitmap bitmap)
{
using (MemoryStream ms = new MemoryStream())
{
///bitmap保存到流对象
bitmap.Save(ms, ImageFormat.Jpeg);
///流对象设置开始位置
ms.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[ms.Length];
///从流对象中读取字节块,将数据写入到字节数组
ms.Read(data, 0, data.Length);
return data;
}
}
}
}
第三步:写一个图片的显示代码
代码如下
<!--图片的显示代码-->
<img id="imgCode" src="/admin/validatecode/getcode" class="codeImage"/>
第四步:引用jQuery的配置文件,再重定向验证码图片链接就可以了
代码如下
<!--引用jQuery的配置文件,再重定向验证码图片链接-->
<script src="~/lib/jquery/jquery.min.js"></script>
<script>
$(function () {
$("#imgCode").click(function () {
console.log(Math.random());
//让img的src重新请求
$(this).attr("src", "/admin/validatecode/getcode?" + Math.random());
});
})
</script>