下面的例子是动态的生成一个图片显示当前时间
namespace zhuohs
{
using System;
using System.Drawing;
using System.Web.UI;
public class MyPicture: Page
{
public string CreateImage()
{
string str=DateTime.Now.ToString();
Bitmap image=new Bitmap(200,30);
Graphics g=Graphics.FromImage(image);
string thefullname=Server.MapPath("/")+"\\nowtime.gif";
g.Clear(Color.White);
g.DrawString(str,new Font("Courier New", 10),new SolidBrush(Color.Red),20,5);
image.Save(thefullname,System.Drawing.Imaging.ImageFormat.Gif);
return "/nowtime.gif";
}
}
} <%@page language="C#"%>
<%@Import namespace="zhuohs"%>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e)
{
MyPicture myTempImage=new MyPicture();
img1.Src=myTempImage.CreateImage();
}
</script>
<html>
<head>
<!--每10秒自动刷新-->
<meta http-equiv="refresh" content="10">
</head>
<body>
<form runat="server">
<input type="button" value="手动刷新" onclick="location.reload()">
<img id="img1" runat="server"/>
</form>
</body>
</html>
Graphics objGraphics = Graphics.FromImage(objBitmap); //Fill the background
objGraphics.FillRectangle(new SolidBrush(Color.LightBlue),0,0,120,30);
//Create blue-yellow bullet point
objGraphics.FillEllipse(new SolidBrush(Color.Blue),3,9,10,10);
objGraphics.FillEllipse(new SolidBrush(Color.Yellow),4,10,8,8);
//Draw text next to bullet point
objGraphics.DrawString("Submit", new Font("Tahoma",8), new SolidBrush(Color.Green), 16,8);
//Render Page.Response.Clear();
Page.Response.ContentType = "image/gif";
objBitmap.Save(Page.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//Tidy up
objGraphics.Dispose();
objBitmap.Dispose();
Graphics graphics=Graphics.FromImage(bitmap); //画一个白色背景
graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
//画一个矩形
Rectangle r = new Rectangle(1, 1, width-1, height-1);
LinearGradientBrush lb = new LinearGradientBrush(r, brushes[style][0], brushes[style][1], LinearGradientMode.Horizontal);
graphics.FillRectangle(lb, r);
//从网页输出
Response.ContentType="image/jpeg";
bitmap.Save(Response.OutputStream,ImageFormat.Jpeg); graphics.Dispose();
bitmap.Dispose();