public class ValidateCode
{
public void CerateValidateGraphic(HttpContext context)
{
//0.0得到字符串
string code = GetVcode(5);
//0.1将验证码保存在Session["对象命名"]中
context.Session["Code"] = code;
//1.0准备一个画板对象装载子类对象绘画纸并确定绘画纸的长宽
using (Bitmap img = new Bitmap((int)Math.Ceiling(code.Length * 13.0), 25))
{
//2.0给画板配备一个画家
using (Graphics g = Graphics.FromImage(img))
{
//将验证码的背景变为白色(默认为黑色)
g.Clear(Color.White);
//绘制边框(即绘图范围),Pens.Black边框宽度
g.DrawRectangle(Pens.Black, 0, 0, img.Width - 1, img.Height - 1);
//画一些干扰点
DrawPoint(g, img, 30);
//话图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = r.Next(img.Width);
int y = r.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(r.Next()));
}
//3.0将验证码划到图片上
g.DrawString(code, new Font("微软雅黑", 12, FontStyle.Italic | FontStyle.Strikeout), Brushes.Red, 0, 0);
}
//3.0将图片保存到Response的输出流中
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
///
/// 绘制干扰线
///
/// 画家对象
/// 画板对象
/// 干扰线数量
public void DrawPoint(Graphics g, Image img, int number)
{
//得到X,Y的坐标
int x;
int y;
int x2;
int y2;
//随机生成x和y的数据,但不能跑出绘画纸范围
for (int i = 0; i < number; i++)
{
x = r.Next(0, img.Width);
x2 = r.Next(0, img.Width);
y = r.Next(0, img.Height);
y2 = r.Next(0, img.Height);
//连接两点,Pens.Black线宽
g.DrawLine(new Pen(Color.Silver), x, y, x2, y2);
}
}
Random r = new Random();
///
/// 得到随机生成的验证码
///
/// 验证码长度
///
public string GetVcode(int number)
{
//准备一个数据源
char[] arr =
{
'0','1','2','3','4','5','6','7','8','9',
'q','w','e','r','t','y','u','i','o','p',
'a','s','d','f','g','h','j','k','l','z',
'x','c','v','b','n','m','Q','W','E','R',
'T','Y','U','I','O','P','A','S','D','F',
'G','H','J','K','L','Z','X','C','V','B',
'N','M'};
// 创建一个验证码字符串
string code = null;
for (int i = 0; i < number; i++)
{
//得到数组的索引
int index = r.Next(0, arr.Length);//包括0,不包括arr.lenght
code += arr[index];
}
return code;
}
//c# MVC升级版
///
/// 创建验证码图片
///
///
public byte[] CreateValidateGraphic(string code)
{
//0.0得到字符串
//1.0准备一个画板对象装载子类对象绘画纸并确定绘画纸的长宽
using (Bitmap img = new Bitmap((int)Math.Ceiling(code.Length * 13.0), 25))
{
//2.0给画板配备一个画家
using (Graphics g = Graphics.FromImage(img))
{
//将验证码的背景变为白色(默认为黑色)
g.Clear(Color.White);
//绘制边框(即绘图范围),Pens.Black边框宽度
g.DrawRectangle(Pens.Black, 0, 0, img.Width - 1, img.Height - 1);
//画一些干扰点
//DrawPoint(g, img, 30);
//话图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = r.Next(img.Width);
int y = r.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(r.Next()));
}
//3.0将验证码划到图片上
g.DrawString(code, new Font("微软雅黑", 12, FontStyle.Italic | FontStyle.Strikeout), Brushes.Red, 0, 0);
}
//保存图片数据
MemoryStream stream = new MemoryStream();
//将图片以流的形式存储为jgep格式
img.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
}
}
///
/// 生成验证码
///
///
public ActionResult ShowValidateCode()
{
ValidateCode validateCode = new ValidateCode();
string code = validateCode.GetVcode(5);//产生验证码
Session["validateCode"] = code;//将验证码数据存储至session中
byte[] buffer = validateCode.CreateValidateGraphic(code);//将验证码画到画布上
return File(buffer, "image/jpeg");//传输图片数据
}
将验证码图片数据传输至前端