添加VerifyCode跟VerifyCodeType类
public class VerifyCode
{
/// <summary>
/// 验证码
/// </summary>
public string Code {
get; set; }
/// <summary>
/// 验证码数据流
/// </summary>
public byte[] Image {
get; set; }
}
public enum VerifyCodeType
{
NUM,
CHAR
}
添加VerifyCodeHelper类
public static class VerifyCodeHelper
{
/// <summary>
/// 获取验证码
/// </summary>
/// <param name="n">验证码数</param>
/// <param name="type">类型 0:数字 1:字符</param>
/// <returns></returns>
public static VerifyCode CreateVerifyCode(int n, VerifyCodeType type)
{
//宽、高,字体大小
int codeW = 74;
int codeH = 36;
int fontSize = 16;
//初始化验证码
string charCode = string.Empty;
switch (type.ToString())
{
case "NUM":
charCode = CreateNumCode(n);
break;
default:
charCode = CreateCharCode(n);
break;
}
//颜色列表
Color[] colors = {
Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
//字体列表
string[] fonts = {
"Times New Roman", "Verdana", "Arial", "Gungsuh" };
//创建画布
Bitmap bitmap = new Bitmap(codeW, codeH);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
Random random = new Random();
//画躁线
for (int i = 0; i < n; i++