protected void Page_Load(object sender, EventArgs e)
{
//生成验证码图片的基本步骤
string checkCode = "新年快乐";
//定义画布大小
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 20.5)), );
//指定绘制类型
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = ; i < ; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
//指定字体/字号/样式
Font font = new System.Drawing.Font("Arial", , (System.Drawing.FontStyle.Bold));
//指定画笔, 线性渐变
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , );
//画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将绘制的图片以指定的格式保存到内存中
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//清除缓存
Response.ClearContent();
//指定输出类型
Response.ContentType = "image/Gif";
//以二进制方式输出
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
} }
最终效果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFMAAAAXCAIAAAD4GeQtAAAEtklEQVRYheVYyw3sNgycTvw7+sha1ALrcBtsgbXMVfUwB0m2tf7sviAIAoQwDNkrUSSHHMoLOpn5f7vP8wxmxp1wE2YBaPn293ehwgQm/idrNhWoJDteuQlEoZE6I623mZucH2W99+iY75znGeb2MklBbKctN+yLAQoMoPjnTnQKT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525DXNLl8Ye0XK+FPOeC6InDmulXvpZx6lM2KPzE7dr7cOlpUVE+4zJxB2333oEmOSj+VvWiNBLHCrOQcH9h4eATKKJX3Z10b6wd8FqcXylXvq5bV94SyHvEw5b49eZ/6Z8wfw33v6Pip7KUC7/anzp5/+wPH8z/i7Wd6a/rYe+f6s56WYP/9fdvbfPN68Dy6RfltzfjZnWxnaosuPeG2Dntfuj26cZVW2dM88zxnGcpmlZlnmep2ma53kYhnme53kex3FZlmmapmkax3F/PwxDmV9+LQuLLMtyVljG+5xhGMq4aD5vumvete027Hr2aeVlmT+OY9G2L9zN27WVx7OdwzD8BSYcbuveQkhwAAAAAElFTkSuQmCC" alt="" />
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// 生成随机数字符串, 并保存到Cookie中
/// </summary>
/// <returns></returns>
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = ; i < ; i++)
{
number = random.Next();
if (number % == )
code = (char)('' + (char)(number % ));
else
code = (char)('A' + (char)(number % ));
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
} protected void Page_Load(object sender, EventArgs e)
{
string checkCode = GenerateCheckCode();
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.3)), );
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = ; i < ; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", , (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , );
//画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
} }
}
最终结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADcAAAAXCAIAAAAgFUAnAAADRElEQVRIic1XyxHkKgzsTOzBRx8VCykoDtJQCsTSV+LRHvj4x8zO1L7DU7lcGAu53YJGgJks/D/fl2UBC/1H04TzI7KNNmNrI2p3ls/RsNuHt8x8vV6w/NbJ0uUVgHeew8T1rz6Hc/qErxoLt21rXEo5Dcbbwe+A0n9OyJdm2UIIYP7tA1Ogkj+llfkgmPHbDynE3VkYQrjPS4sEiNRdd3F3S7RIgUluQJkpsHalHqFQoXBoPlBZVIVKZHuEKIS7KWRcgmb334OxcF3Xx7zMDhC4prBQQAF1dwOZ7IB4BcqiChNYfUaCQBVqpYOGKlQh/d4uvskG84xLL94A5VNnJkCJpLs7FZTM9uuZAjOXEw4TqCZ2TKaJnSwRiELtFJxJuFOhLG4PwbGmRHnQ0BoW/Zx0d7d4wt1yTbY5WmEZ3QFxd8+mMIEKTKECEUCbMFXc45c60CgKmapNU6KJXjbmetJ7um+hqwF6Bg2ASQRaEy1X2vq8vHW6Z1GIxhnKYiGEmV4Wt6IYsC7pbiZFNFqHaAIB4D5CUSuR8UbBnEvPphDd7eZtCZd5qddwtlPgNcW2H+nGxcQaRArA/aTnyTqXxg4I2Q6UNy6LKmSq0iw145n6VMBMgIgOqEcH7KEUVDQxGtGrgyYq6krXB50943cudcql4N0ab98zgExqkQKRhxRbtDEd66MXc3dALKokk0jPxmzXWTjnknvtpLszXz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