使用我新做的验证码控件就行了,免去好多麻烦。
WakiCheckDigit for asp.net 是适合于微软.Net开发平台的Web控件。它能放进Web 工具箱中,并可拖入 WEB 页面进行可视化的设置。它可以方便快速生成验证码,用于用户登录,身份验证等。可防止程序穷举破解用户信息。不用编程,一分钟生成安全验证系统,为你的系统构筑多一道安全防线。
下载地址:
http://www.aspxcontrol.com/download/wakicheckdigit.rar
public static void ValidateCode( Font font, Color foreColor, Color backColor, Stream stream )
{
ValidateCode( MakeValidateCode(4), font, foreColor, backColor, stream );
}
public static void ValidateCode( string str, Font font, Color foreColor, Color backColor, Stream stream)
{
Bitmap paper = new Bitmap( (int) font.SizeInPoints * System.Text.Encoding.GetEncoding("gb2312").GetByteCount( str ), font.Height, PixelFormat.Format24bppRgb );//创建画布
#region 作图
Graphics g = Graphics.FromImage( paper );
//填充背景
g.FillRectangle( new SolidBrush( backColor ), 0,0, paper.Width, paper.Height );
//画出字符
g.DrawString( str, font, new SolidBrush( foreColor ), new PointF( 0, 0 ) );
//添加干扰线
g.DrawBezier( new Pen( new SolidBrush( foreColor ), 1 ), GetRandomPoint(0, paper.Height), new Point( paper.Width ,paper.Height), GetRandomPoint(paper.Width, paper.Height), GetRandomPoint(paper.Width, paper.Height) );
//完成保存
g.Save();
#endregion
paper.Save( stream, ImageFormat.Png );
}
private static Point GetRandomPoint ( int x, int y )
{
return new Point( _rand.Next( x ), _rand.Next( y ) );
}
public static string MakeValidateCode(int length)
{
// char[] s = new char[]{'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'};
char[] s = @"1234567890".ToCharArray();
string num = "";
Random r = new Random();
for(int i = 0; i < length; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}
Random rand = new Random();
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage也是一个自定义的函数,用于生成图
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.White);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen,0,y,image.Width,y);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
这两种方法都可以改变生成图片的背景颜色
下面那个for循环用来生成一些随机的水平线