以下是c#生成验证码的代码。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BitCar.Finance.C2C.Common
{
public class ValidateCodeHelper
{
<summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
//定义一个包括数字、大写英文字母和小写英文字母的字符串
string strchar = "0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,6,8";
//将strchar字符串转化为数组
//String.Split 方法返回包含此实例中的子字符串(由指定Char数组的元素分隔)的 String 数组。
string[] VcArray = strchar.Split(',');
string VNum = "";
//记录上次随机数值,尽量避免产生几个一样的随机数
int temp = -1;
//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 1; i < length + 1; i++)
{
if (temp != -1)
{
//unchecked 关键字用于取消整型算术运算和转换的溢出检查。
//DateTime.Ticks 属性获取表示此实例的日期和时间的刻度数。
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
//Random.Next 方法返回一个小于所指定最大值的非负随机数。
int t = rand.Next(61);
if (temp != -1 && temp == t)
{
return CreateValidateCode(length);
}
temp = t;
VNum += VcArray[t];
}
return VNum;//返回生成的随机数
}
// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public byte[] CreateValidateGraphic(string validateCode)
{
Bitmap imge = new Bitmap(validateCode.Length * 12 + 10, 22);
Graphics g = Graphics.FromImage(imge);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(imge.Width);
int x2 = random.Next(imge.Width);
int y1 = random.Next(imge.Height);
int y2 = random.Next(imge.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, imge.Width, imge.Height), Color.BlueViolet, Color.Crimson, 1.2f, true);
g.DrawString(validateCode, font, brush, 2, 2);
for (int i = 1; i < 80; i++)
{
int x = random.Next(imge.Width);
int y = random.Next(imge.Height);
imge.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, imge.Width - 1, imge.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
imge.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
imge.Dispose();
}
}
// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public byte[] CreateValidateGraphic2(string validateCode)
{
Bitmap imge = new Bitmap(validateCode.Length * 12 + 50, 40);
Graphics g = Graphics.FromImage(imge);
try
{
Random random = new Random();
g.Clear(Color.FromArgb(238, 238, 238));
//for (int i = 0; i < 25; i++)
//{
// int x1 = random.Next(imge.Width);
// int x2 = random.Next(imge.Width);
// int y1 = random.Next(imge.Height);
// int y2 = random.Next(imge.Height);
// g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
//}
Font font = new Font("微软雅黑", 22, (FontStyle.Regular));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, imge.Width, imge.Height), Color.Black, Color.Black, 1.2f, true);
g.DrawString(validateCode, font, brush, 2, 2);
//for (int i = 1; i < 80; i++)
//{
// int x = random.Next(imge.Width);
// int y = random.Next(imge.Height);
// imge.SetPixel(x, y, Color.FromArgb(random.Next()));
//}
g.DrawRectangle(new Pen(Color.FromArgb(238, 238, 238)), 0, 0, imge.Width - 1, imge.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
imge.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
imge.Dispose();
}
}
}
}