
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "image/jpeg";//输出流的 HTTP MIME 类型。默认值为“text/html”。
Response.Clear();//清除缓冲区流中的所有内容输出。
Response.BufferOutput = true;//获取或设置一个值,该值指示是否缓冲输出并在处理完整个页之后发送它。
Font rectangleFont = new Font("Arial", 10, FontStyle.Bold);//使用指定的大小和样式初始化新 System.Drawing.Font。
int height = 25;//图片的高度
int width = 150;//图片的宽度
Random r = new Random();//使用与时间相关的默认种子值,初始化 System.Random 类的新实例。
int x = r.Next(75);//返回一个小于所指定最大值的非负随机数。
int a = r.Next(155);
int x1 = r.Next(100);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);//用指定的大小和格式初始化 System.Drawing.Bitmap 类的新实例。
Graphics g = Graphics.FromImage(bmp);//此方法为指定的 System.Drawing.Image 返回一个新的 System.Drawing.Graphics。
g.SmoothingMode = SmoothingMode.AntiAlias;//指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
g.Clear(Color.Coral);//清除整个绘图面并以指定背景色填充。
//在指定位置并且用指定的 System.Drawing.Brush 和 System.Drawing.Font 对象绘制指定的文本字符串。
g.DrawString("ASP.NET 验证码", rectangleFont, SystemBrushes.WindowText, new PointF(16, 5));
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);//将此图像以指定的格式保存到指定的流中。
g.Dispose();//释放由 System.Drawing.Graphics 使用的所有资源。
bmp.Dispose();//释放由 System.Drawing.Image 使用的所有资源。
Response.Flush();//向客户端发送当前所有缓冲的输出。
}
}
