<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
private void Page_Load(object sender, EventArgs e)
...{
//设置页面的content type为JPEG文件
//并且清除所有的响应头部信息
Response.ContentType = "image/jpeg";
Response.Clear();
//对响应作出缓冲以便处理完成后发送页面
Response.BufferOutput = true;
//创建一字体风格
Font rectangleFont = new Font(
"Arial", 10, FontStyle.Bold);
//创建整数变量
int height = 100;
int width = 200;
//创建一个随机数字生成器并且基于它创建
//变量值
Random r = new Random();
int x = r.Next(75);
int a = r.Next(155);
int x1 = r.Next(100);
//创建一张位图并且使用它创建一个
//Graphics对象
Bitmap bmp = new Bitmap(
width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.LightGray);
//使用这个Graphics对象绘制3个矩形
g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
g.DrawRectangle(Pens.Black, 0, 0, width, height);
//使用这个Graphics对象输出一个字符串
// on the rectangles.
g.DrawString(
"ASP.NET Samples", rectangleFont,
SystemBrushes.WindowText, new PointF(10, 40));
//在其中两个矩形上添加颜色
g.FillRectangle(
new SolidBrush(
Color.FromArgb(a, 255, 128, 255)),
x, 20, 100, 50);
g.FillRectangle(
new LinearGradientBrush(
new Point(x, 10),
new Point(x1 + 75, 50 + 30),
Color.FromArgb(128, 0, 0, 128),
Color.FromArgb(255, 255, 255, 240)),
x1, 50, 75, 30);
//把位图保存到响应流中并且把它转换成JPEG格式
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
//释放掉Graphics对象和位图所使用的内存空间
g.Dispose();
bmp.Dispose();
//把输出结果发送到客户端
Response.Flush();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
</form>
</body>
</html>
|