调用:
public ActionResult Vcode()//验证码
{
string code = ValidateCode.CreateRandomCode(4);
ValidateCode.CreateImage(code);
Session["vcode"] = code.ToLower();//存入session供验证用
System.IO.MemoryStream ms = new System.IO.MemoryStream();
return File(ms.GetBuffer(), "image/JPEG");
}
//下面是主题class内部
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;
namespace xxxxxx.Models//命名空间自己填咯
{
public class ValidateCode
{
public static string CreateRandomCode(int length)
{
int rand;
char code;
string randomcode = String.Empty;
//生成一定长度的验证码
System.Random random = new Random();
for (int i = 0; i < length; i++)
{
rand = random.Next();
if (rand % 3 == 0)
{
code = (char)('A' + (char)(rand % 26));//如果是3的倍数就除以26将余数(小于26)转换成大写字母
}
else if (rand % 7 == 0)
{
code = (char)('a' + (char)(rand % 26));
}
else
{
code = (char)('0' + (char)(rand % 10));
}
randomcode += code.ToString();
}
return randomcode;
}
public static void CreateImage(string randomcode)
{
int randAngle = 30; //随机转动角度范围
int mapwidth = (int)(randomcode.Length * 23);//背景的宽度,此值是你输入要求的字符长度的23倍大(4*23=92宽)
using (Bitmap map = new Bitmap(mapwidth, 28))//创建图片背景,宽为(4*28),高为28的画布,从右上角(0,0)处开始;
{
Random rand = new Random();//随机数实例
using (Bitmap temp = new Bitmap(map.Width, map.Height))//临时画布
{
using (Graphics tempGra = Graphics.FromImage(temp))
{
char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组
StringFormat format = new StringFormat(StringFormatFlags.NoClip);//定义文字格式
format.Alignment = StringAlignment.Center;//文字距中
format.LineAlignment = StringAlignment.Center;
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定义颜色
string[] fontList = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };//定义字体
for (int i = 0; i < chars.Length; i++)//逐个绘制文字
{
int cindex = rand.Next(7);//颜色随机数
int findex = rand.Next(5);//字体随机数
Font font = new System.Drawing.Font(fontList[findex], 13, System.Drawing.FontStyle.Bold);//字体样式(参数2为字体大小)
Brush brush = new System.Drawing.SolidBrush(c[cindex]);//文字颜色1
//Brush brushPen = new LinearGradientBrush(new Rectangle(0, 0, temp.Width, temp.Height), Color.FromArgb(rand.Next(0, 256), 0, 0), Color.FromArgb(0, 0, rand.Next(0, 256)), rand.Next(90));//文字颜色2渐变
Point dot = new Point(16, 14);//定义一个点
//graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//测试X坐标显示间距的
float angle = rand.Next(-randAngle, randAngle);//转动的度数
tempGra.TranslateTransform(dot.X, dot.Y);//更改坐标系的原点(所有的操作在此原点(16,16))
tempGra.RotateTransform(angle);//旋转画布
//Matrix m = new Matrix();//创建变换
//m.RotateAt(angle, new PointF(16, 16), MatrixOrder.Append);//旋转
//m.Shear(rand.Next(-10, 10) * 0.03f, 0);//扭曲
//tempGra.Transform = m;//画布应用变换
tempGra.DrawString(chars[i].ToString(), font, brush, 1, 1, format);//开始画字母或文字
tempGra.RotateTransform(-angle);//转回去
tempGra.TranslateTransform(4, -dot.Y);//更改坐标系的原点
}
}
using (Graphics graph = Graphics.FromImage(map))//创建目标画布;
{
graph.Clear(Color.AliceBlue);//清除画面,填充背景
Rectangle rect = new Rectangle(0, 0, map.Width, map.Height);//绘制渐变背景
Brush brushBack = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(150, 256), 255, 255), Color.FromArgb(255, rand.Next(150, 256), 255), rand.Next(90));//渐变背景颜色
graph.FillRectangle(brushBack, rect);//背景填充到画布
graph.DrawRectangle(new Pen(Color.LightPink, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框,默认是从屏幕右上角(0,0)初开始画
//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//图片抗锯齿模式
Pen blackPen = new Pen(Color.LightGray, 0);//笔触颜色
for (int i = 0; i < 50; i++)//噪点50个
{
int x = rand.Next(1, map.Width - 3);
int y = rand.Next(1, map.Height - 3);
graph.DrawRectangle(blackPen, x, y, 1, 1);//生成噪点
}
graph.DrawImage(temp, new Point(0, 0));//先画点后画图
Pen pen = new Pen(Color.Gray, 1);//干扰线笔触
for (int i = 0; i < 2; i++)//绘制两条干扰线
{
Point p1 = new Point(0, rand.Next(map.Height));
Point p2 = new Point(rand.Next(map.Width), rand.Next(map.Height));
Point p3 = new Point(rand.Next(map.Width), rand.Next(map.Height));
Point p4 = new Point(map.Width, rand.Next(map.Height));
Point[] p = { p1, p2, p3, p4 };
graph.DrawBeziers(pen, p);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
temp.Dispose();//释放
graph.Dispose();
map.Dispose();
}
}
}
}
}
}