[c#]代码库
public partial class ValidateImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
char[] chars = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();
System.Random random = new Random();
string validateCode = string.Empty;
for (int i = 0; i < 4; i++)
{
char rc = chars[random.Next(0, chars.Length)];
if (validateCode.IndexOf(rc) > -1)
{
i--;
continue;
}
validateCode += rc;
}
Session["ValidateCode"] = validateCode;
CreateImage(validateCode);
}
/// <summary>
/// 创建图片
/// </summary>
/// <param name="checkCode"></param>
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 19);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Chocolate, Color.Brown, Color.DarkCyan, Color.Purple };
Random rand = new Random();
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(7);
Font f = new System.Drawing.Font("Microsoft Sans Serif", 11);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
g.DrawString(checkCode.Substring(i, 1), f, b, (i * 10) + 1, 0, StringFormat.GenericDefault);
}
//画一个边框
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);
//输出到浏览器
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();
}
}