结合从网上收集的代码,整理了验证码生成的功能。
源代码来源于网络,个人进行了综合更改,非原创
Code
/**//// <summary>
/// 生成随机验证码
/// </summary>
public class VerifyCode
{
private Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
private String _cookieName = "CheckCode";
public String CookieName
{
get { return _cookieName; }
set { _cookieName = value; }
}
属性变量#region 属性变量
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
private bool _chaoLine = false;
/**//// <summary>
/// 产生波形
/// </summary>
private bool _chaoWave = false;
/**//// <summary>
/// 是否输出燥点(默认不输出)
/// </summary>
bool _chaoPoint = false;
/**//// <summary>
/// 验证码大小
/// </summary>
int _fontSize = 30;
/**//// <summary>
/// 边框
/// </summary>
int _padding = 2;
/**//// <summary>
/// 噪点颜色
/// </summary>
Color _chaoPointColor = Color.LightGray;
/**//// <summary>
/// 噪点数量
/// </summary>
int _chaoPointCount = 100;
/**//// <summary>
/// 背景颜色
/// </summary>
Color _backgroundColor = Color.White;
/**//// <summary>
/// 颜色数组
/// </summary>
Color[] _colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
/**//// <summary>
/// 字体数组
/// </summary>
string[] _fonts = { "Arial", "Georgia", "宋体", "黑体" };
#endregion
公共属性设置#region 公共属性设置
/**//// <summary>
/// 产生波形扭曲
/// </summary>
public bool ChaoWave
{
get { return _chaoWave; }
set { _chaoWave = value; }
}
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
public bool ChaoLine
{
get { return _chaoLine; }
set { _chaoLine = value; }
}
/**//// <summary>
/// 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)
/// 验证码字体大小(默认为40默认40像素)
/// </summary>
public int FontSize
{
get { return _fontSize; }
set { _fontSize = value; }
}
/**//// <summary>
/// 边框像素(默认为2)
/// </summary>
public int Padding
{
get { return _padding; }
set { _padding = value; }
}
/**//// <summary>
/// 是否输出燥点(默认为不是)
/// </summary>
public bool ChaoPoint
{
get { return _chaoPoint; }
set { _chaoPoint = value; }
}
/**//// <summary>
/// 输出燥点的颜色(默认Color.LightGray)
/// </summary>
public Color ChaoPointColor
{
get { return _chaoPointColor; }
set { _chaoPointColor = value; }
}
/**//// <summary>
/// 燥点数量(默认为100)
/// </summary>
public int ChaoPointCount
{
get { return _chaoPointCount; }
set { _chaoPointCount = value; }
}
/**//// <summary>
/// 背景颜色,默认为白色
/// </summary>
public Color BackgroundColor
{
get { return _backgroundColor; }
set { _backgroundColor = value; }
}
/**//// <summary>
/// 随机颜色数组Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple
/// </summary>
public Color[] Colors
{
get { return _colors; }
set { _colors = value; }
}
/**//// <summary>
/// 字体数组"Arial", "Georgia", "宋体", "黑体"
/// </summary>
public string[] Fonts
{
get { return _fonts; }
set { _fonts = value; }
}
#endregion
产生波形滤镜效果并画一个边框#region 产生波形滤镜效果并画一个边框
// <summary>
// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
// </summary>
// <param name="srcBmp">图片路径</param>
// <param name="bXDir">如果扭曲则选择为True</param>
// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
// <returns></returns>
private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
//const double PI = 3.1415926535897932384626433832795;
const double PI2 = 6.283185307179586476925286766559;
Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
Graphics g = System.Drawing.Graphics.FromImage(destBmp);
g.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
//渐变效果
//g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(destBmp.Width, destBmp.Height), Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 255, 255, 255)), 0, 0, destBmp.Width, destBmp.Height);
//画边框
g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, destBmp.Width - 1, destBmp.Height - 1);
g.Dispose();
return destBmp;
}
#endregion
生成验证码码图片#region 生成验证码码图片
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
/// <param name="image"></param>
/// <param name="g"></param>
private void CreateImageLine(Bitmap image, Graphics g)
{
if (_chaoLine)
{
for (int i = 0; i < 25; i++)
{
int x1 = rnd.Next(image.Width);
int x2 = rnd.Next(image.Width);
int y1 = rnd.Next(image.Height);
int y2 = rnd.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
}
}
/**//// <summary>
/// 写入每一个字符
/// </summary>
/// <param name="code"></param>
/// <param name="fSize"></param>
/// <param name="imageHeight"></param>
/// <param name="g"></param>
private void CreateImageString(string code, int fSize, int imageHeight, Graphics g)
{
//定义位移(左右,上下)
int left = 0, top = 0, top1 = 1, top2 = 1;
//
int n2 = (imageHeight - FontSize - Padding * 2) / 5;
top1 = n2;
top2 = n2 * 2;
Font f;
Brush b;
int cindex, findex;//颜色和字体
for (int i = 0; i < code.Length; i++)
{
cindex = rnd.Next(Colors.Length - 1);//从颜色数组中随机取出一个
findex = rnd.Next(Fonts.Length - 1); //从字体数组中随机取出一个
f = new Font(Fonts[findex], fSize, FontStyle.Bold);
b = new SolidBrush(Colors[cindex]);
//产生、随机移位
if (i % 2 == 1)
{
top = top2;
}
else
{
top = top1;
}
int sub;
sub = rnd.Next(4, 8);
if (sub > 5)
{
sub = -sub;
}
//---------
if (i != 0) //第一个字不位移
{
left = i * (FontSize + Padding) + sub;
top += sub;
}
//----------
/**////添加文本字符串
left = i * (FontSize + Padding);
top = Padding;
g.DrawString(code.Substring(i, 1), f, b, left, top);
}
}
/**//// <summary>
/// 生成随机噪点
/// </summary>
/// <param name="image"></param>
/// <param name="g"></param>
private void CreateImagePoint(Bitmap image, Graphics g)
{
if (_chaoPoint)
{
Pen pen = new Pen(ChaoPointColor, 0);
int c = _chaoPointCount;
for (int i = 0; i < c; i++)
{
int x = rnd.Next(image.Width);
int y = rnd.Next(image.Height);
g.DrawRectangle(pen, x, y, 1, 1);
}
}
}
/**//// <summary>
/// 生成扭曲变形
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private Bitmap CreateImageWave(Bitmap image)
{
if (_chaoWave)
{
//随机生成后三个参数(true or false,5~8,(0~2PI),)
int _int1 = rnd.Next(4, 10);
int _int2 = rnd.Next(1, 6);
bool _bool = _int2 > 3 ? true : false;
image = TwistImage(image, _bool, _int1, _int2);
}
return image;
}
/**//// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="code">验证码</param>
/// <returns>返回Bitmap</returns>
private Bitmap CreateImageCode(string code)
{
//字体大小
int fSize = FontSize + Padding;
//图片宽度
int imageWidth = (int)(code.Length * fSize) + 10 + Padding * 2;
//图片高度
int imageHeight = FontSize * 2 + Padding;
//创建图像
Bitmap image = new Bitmap(imageWidth, imageHeight);
// 填充背景颜色
Graphics g = Graphics.FromImage(image);
g.Clear(BackgroundColor);
CreateImageLine(image, g);
CreateImageString(code, fSize, imageHeight, g);
CreateImagePoint(image, g);
image = CreateImageWave(image);
//g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
g.Dispose();
return image;
}
#endregion
将创建好的图片输出到页面#region 将创建好的图片输出到页面
/**//**/
/**//// <summary>
/// 把字符图片输出到页面
/// </summary>
/// <param name="code">字符</param>
/// <param name="context">页面</param>
public void CreateImageOnPage(string code, System.Web.HttpContext httpcontext)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Bitmap image = this.CreateImageCode(code);
httpcontext.Response.Cookies.Add(new System.Web.HttpCookie(_cookieName, code));
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
httpcontext.Response.ClearContent();
httpcontext.Response.ContentType = "image/Jpeg";
httpcontext.Response.BinaryWrite(ms.GetBuffer());
ms.Close();
ms = null;
image.Dispose();
image = null;
}
#endregion
/**//// <summary>
/// 生成汉字验证码
/// </summary>
/// <param name="nLength"></param>
/// <param name="page"></param>
public void GNChinese(Int32 nLength, System.Web.HttpContext httpcontext)
{
产生中文#region 产生中文
//获取GB2312编码页(表)
Encoding gb = Encoding.GetEncoding("gb2312");
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
//定义一个object数组用来
//String[] StrCode=new String[strlength];
String StrCode = "";
/**//**/
/**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
for (int i = 0; i < nLength; i++)
{
//区位码第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
//区位码第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
//区位码第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
//将两个字节变量存储在字节数组中
byte[] str_r = new byte[] { byte1, byte2 };
//根据汉字编码的字节数组解码出中文汉字
String character = gb.GetString((byte[])Convert.ChangeType(str_r, typeof(byte[])));
//将产生的一个汉字的字节数组放入object数组中
StrCode += character;
//StrCode.SetValue(character, i);
}
#endregion
CreateImageOnPage(StrCode, httpcontext);
}
/**//// <summary>
/// 生成字母和数字混合验证码图像
/// </summary>
/// <param name="nLength"></param>
/// <param name="httpcontext"></param>
public void GNCode(Int32 nLength,System.Web.HttpContext httpcontext)
{
CreateImageOnPage(RndNum(nLength), httpcontext);
}
/**//// <summary>
/// 生成字母和数字混合验证码
/// </summary>
/// <param name="VcodeNum"></param>
/// <returns></returns>
private string RndNum(int VcodeNum)
{
string Vchar = "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,x,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,X,Y,Z";
string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成数组
string VNum = "";
int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数
Random rand = new Random();
//采用一个简单的算法以保证生成随机数的不同
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
}
/**//// <summary>
/// 生成随机验证码
/// </summary>
public class VerifyCode
{
private Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
private String _cookieName = "CheckCode";
public String CookieName
{
get { return _cookieName; }
set { _cookieName = value; }
}
属性变量#region 属性变量
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
private bool _chaoLine = false;
/**//// <summary>
/// 产生波形
/// </summary>
private bool _chaoWave = false;
/**//// <summary>
/// 是否输出燥点(默认不输出)
/// </summary>
bool _chaoPoint = false;
/**//// <summary>
/// 验证码大小
/// </summary>
int _fontSize = 30;
/**//// <summary>
/// 边框
/// </summary>
int _padding = 2;
/**//// <summary>
/// 噪点颜色
/// </summary>
Color _chaoPointColor = Color.LightGray;
/**//// <summary>
/// 噪点数量
/// </summary>
int _chaoPointCount = 100;
/**//// <summary>
/// 背景颜色
/// </summary>
Color _backgroundColor = Color.White;
/**//// <summary>
/// 颜色数组
/// </summary>
Color[] _colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
/**//// <summary>
/// 字体数组
/// </summary>
string[] _fonts = { "Arial", "Georgia", "宋体", "黑体" };
#endregion
公共属性设置#region 公共属性设置
/**//// <summary>
/// 产生波形扭曲
/// </summary>
public bool ChaoWave
{
get { return _chaoWave; }
set { _chaoWave = value; }
}
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
public bool ChaoLine
{
get { return _chaoLine; }
set { _chaoLine = value; }
}
/**//// <summary>
/// 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)
/// 验证码字体大小(默认为40默认40像素)
/// </summary>
public int FontSize
{
get { return _fontSize; }
set { _fontSize = value; }
}
/**//// <summary>
/// 边框像素(默认为2)
/// </summary>
public int Padding
{
get { return _padding; }
set { _padding = value; }
}
/**//// <summary>
/// 是否输出燥点(默认为不是)
/// </summary>
public bool ChaoPoint
{
get { return _chaoPoint; }
set { _chaoPoint = value; }
}
/**//// <summary>
/// 输出燥点的颜色(默认Color.LightGray)
/// </summary>
public Color ChaoPointColor
{
get { return _chaoPointColor; }
set { _chaoPointColor = value; }
}
/**//// <summary>
/// 燥点数量(默认为100)
/// </summary>
public int ChaoPointCount
{
get { return _chaoPointCount; }
set { _chaoPointCount = value; }
}
/**//// <summary>
/// 背景颜色,默认为白色
/// </summary>
public Color BackgroundColor
{
get { return _backgroundColor; }
set { _backgroundColor = value; }
}
/**//// <summary>
/// 随机颜色数组Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple
/// </summary>
public Color[] Colors
{
get { return _colors; }
set { _colors = value; }
}
/**//// <summary>
/// 字体数组"Arial", "Georgia", "宋体", "黑体"
/// </summary>
public string[] Fonts
{
get { return _fonts; }
set { _fonts = value; }
}
#endregion
产生波形滤镜效果并画一个边框#region 产生波形滤镜效果并画一个边框
// <summary>
// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
// </summary>
// <param name="srcBmp">图片路径</param>
// <param name="bXDir">如果扭曲则选择为True</param>
// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
// <returns></returns>
private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
//const double PI = 3.1415926535897932384626433832795;
const double PI2 = 6.283185307179586476925286766559;
Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
Graphics g = System.Drawing.Graphics.FromImage(destBmp);
g.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
//渐变效果
//g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(destBmp.Width, destBmp.Height), Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 255, 255, 255)), 0, 0, destBmp.Width, destBmp.Height);
//画边框
g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, destBmp.Width - 1, destBmp.Height - 1);
g.Dispose();
return destBmp;
}
#endregion
生成验证码码图片#region 生成验证码码图片
/**//// <summary>
/// 画图片的背景噪音线
/// </summary>
/// <param name="image"></param>
/// <param name="g"></param>
private void CreateImageLine(Bitmap image, Graphics g)
{
if (_chaoLine)
{
for (int i = 0; i < 25; i++)
{
int x1 = rnd.Next(image.Width);
int x2 = rnd.Next(image.Width);
int y1 = rnd.Next(image.Height);
int y2 = rnd.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
}
}
/**//// <summary>
/// 写入每一个字符
/// </summary>
/// <param name="code"></param>
/// <param name="fSize"></param>
/// <param name="imageHeight"></param>
/// <param name="g"></param>
private void CreateImageString(string code, int fSize, int imageHeight, Graphics g)
{
//定义位移(左右,上下)
int left = 0, top = 0, top1 = 1, top2 = 1;
//
int n2 = (imageHeight - FontSize - Padding * 2) / 5;
top1 = n2;
top2 = n2 * 2;
Font f;
Brush b;
int cindex, findex;//颜色和字体
for (int i = 0; i < code.Length; i++)
{
cindex = rnd.Next(Colors.Length - 1);//从颜色数组中随机取出一个
findex = rnd.Next(Fonts.Length - 1); //从字体数组中随机取出一个
f = new Font(Fonts[findex], fSize, FontStyle.Bold);
b = new SolidBrush(Colors[cindex]);
//产生、随机移位
if (i % 2 == 1)
{
top = top2;
}
else
{
top = top1;
}
int sub;
sub = rnd.Next(4, 8);
if (sub > 5)
{
sub = -sub;
}
//---------
if (i != 0) //第一个字不位移
{
left = i * (FontSize + Padding) + sub;
top += sub;
}
//----------
/**////添加文本字符串
left = i * (FontSize + Padding);
top = Padding;
g.DrawString(code.Substring(i, 1), f, b, left, top);
}
}
/**//// <summary>
/// 生成随机噪点
/// </summary>
/// <param name="image"></param>
/// <param name="g"></param>
private void CreateImagePoint(Bitmap image, Graphics g)
{
if (_chaoPoint)
{
Pen pen = new Pen(ChaoPointColor, 0);
int c = _chaoPointCount;
for (int i = 0; i < c; i++)
{
int x = rnd.Next(image.Width);
int y = rnd.Next(image.Height);
g.DrawRectangle(pen, x, y, 1, 1);
}
}
}
/**//// <summary>
/// 生成扭曲变形
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private Bitmap CreateImageWave(Bitmap image)
{
if (_chaoWave)
{
//随机生成后三个参数(true or false,5~8,(0~2PI),)
int _int1 = rnd.Next(4, 10);
int _int2 = rnd.Next(1, 6);
bool _bool = _int2 > 3 ? true : false;
image = TwistImage(image, _bool, _int1, _int2);
}
return image;
}
/**//// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="code">验证码</param>
/// <returns>返回Bitmap</returns>
private Bitmap CreateImageCode(string code)
{
//字体大小
int fSize = FontSize + Padding;
//图片宽度
int imageWidth = (int)(code.Length * fSize) + 10 + Padding * 2;
//图片高度
int imageHeight = FontSize * 2 + Padding;
//创建图像
Bitmap image = new Bitmap(imageWidth, imageHeight);
// 填充背景颜色
Graphics g = Graphics.FromImage(image);
g.Clear(BackgroundColor);
CreateImageLine(image, g);
CreateImageString(code, fSize, imageHeight, g);
CreateImagePoint(image, g);
image = CreateImageWave(image);
//g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
g.Dispose();
return image;
}
#endregion
将创建好的图片输出到页面#region 将创建好的图片输出到页面
/**//**/
/**//// <summary>
/// 把字符图片输出到页面
/// </summary>
/// <param name="code">字符</param>
/// <param name="context">页面</param>
public void CreateImageOnPage(string code, System.Web.HttpContext httpcontext)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Bitmap image = this.CreateImageCode(code);
httpcontext.Response.Cookies.Add(new System.Web.HttpCookie(_cookieName, code));
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
httpcontext.Response.ClearContent();
httpcontext.Response.ContentType = "image/Jpeg";
httpcontext.Response.BinaryWrite(ms.GetBuffer());
ms.Close();
ms = null;
image.Dispose();
image = null;
}
#endregion
/**//// <summary>
/// 生成汉字验证码
/// </summary>
/// <param name="nLength"></param>
/// <param name="page"></param>
public void GNChinese(Int32 nLength, System.Web.HttpContext httpcontext)
{
产生中文#region 产生中文
//获取GB2312编码页(表)
Encoding gb = Encoding.GetEncoding("gb2312");
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
//定义一个object数组用来
//String[] StrCode=new String[strlength];
String StrCode = "";
/**//**/
/**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
for (int i = 0; i < nLength; i++)
{
//区位码第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
//区位码第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
//区位码第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
//将两个字节变量存储在字节数组中
byte[] str_r = new byte[] { byte1, byte2 };
//根据汉字编码的字节数组解码出中文汉字
String character = gb.GetString((byte[])Convert.ChangeType(str_r, typeof(byte[])));
//将产生的一个汉字的字节数组放入object数组中
StrCode += character;
//StrCode.SetValue(character, i);
}
#endregion
CreateImageOnPage(StrCode, httpcontext);
}
/**//// <summary>
/// 生成字母和数字混合验证码图像
/// </summary>
/// <param name="nLength"></param>
/// <param name="httpcontext"></param>
public void GNCode(Int32 nLength,System.Web.HttpContext httpcontext)
{
CreateImageOnPage(RndNum(nLength), httpcontext);
}
/**//// <summary>
/// 生成字母和数字混合验证码
/// </summary>
/// <param name="VcodeNum"></param>
/// <returns></returns>
private string RndNum(int VcodeNum)
{
string Vchar = "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,x,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,X,Y,Z";
string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成数组
string VNum = "";
int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数
Random rand = new Random();
//采用一个简单的算法以保证生成随机数的不同
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
}