public class ValidateCode : WebControl
{
/// <summary>
/// 默认构造函数,暴露的属性接口
/// </summary>
public ValidateCode()
{
this.CharCount = ;
this.Width = ;
this.Height = ;
CodeColor = Color.White;
} /// <summary>
/// 设置参数
/// </summary>
/// <param name="CharCount"></param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="color"></param>
public ValidateCode(int CharCount, int Width, int Height,Color color)
{
this.CharCount = CharCount;
this.Width = Width;
this.Height = Height;
this.CodeColor = color;
}
/// <summary>
/// 字符个数
/// </summary>
public int CharCount
{
get;
set;
}
/// <summary>
/// 图标矩形宽度
/// </summary>
public new int Width
{
set;
get;
}
/// <summary>
/// 图标矩形高度
/// </summary>
public new int Height
{
set;
get;
} /// <summary>
/// Code背景色
/// </summary>
public Color CodeColor
{
get;
set;
} /// <summary>
/// 验证验证码是否正确
/// </summary>
/// <param name="sn"></param>
/// <returns></returns>
public bool checkCode(string sn)
{
return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Values["ChkCode"].ToString().ToUpper());//读取Cookie
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.DesignMode)
return;
string str = this.Page.Request.QueryString["_xuImageTag"]; if (str != "")
return;
HttpResponse resp = this.Page.Response;
string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange }; string[] font = { "宋体", "Times New Roman", "MS Mincho", "楷体", "隶书", "微软雅黑","Calibri" };
char[] character ={'','','','','','','','','','','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'};
Random rnd = new Random();
for (int i = ; i < this.CharCount; i++)
{
chkCode += character[rnd.Next(character.Length)];
} resp.Cookies["validateCookie"].Values["ChkCode"] =chkCode;//验证码写入Cookie Bitmap bmp = new Bitmap(this.Width,this.Height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(this.CodeColor); //画噪线
for (int i = ; i < ; i++)
{
int x1 = rnd.Next(this.Width);
int y1 = rnd.Next(this.Height);
int x2 = rnd.Next(this.Width);
int y2 = rnd.Next(this.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证字符串
for (int i = ; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, );
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * this.Width/chkCode.Length++, ((float)this.Height)/);
} for (int i = ; i < (this.Width*this.Height)/; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
} //将验证图片写入内存流,并将以image/Png格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
resp.ClearContent();
resp.ContentType = "image/Png";
resp.BinaryWrite(ms.ToArray());
resp.Flush();
resp.End();
}
finally
{
bmp.Dispose();
g.Dispose();
} }
protected override void Render(HtmlTextWriter writer)
{ if (!this.DesignMode)
{
writer.Write("<img border=\"0\" src=\"{0}\">", this.Page.Request.Path + "?_xuImageTag=1");
}
else
{
writer.Write("验证码");
}
base.Render(writer);
}
}