本文介紹了在ASP.NET中動態(tài)生成圖片的方法。下面的例子是動態(tài)的生成一個顯示當(dāng)前時(shí)間的圖片。
namespace Wmj
{
using System;
using System.Drawing;
using System.Web.UI;
public class MyTempImage : 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);
//Graphics 類還有很多可以繪制 直線、曲線、圓等等方法
image.Save(thefullname,System.Drawing.Imaging.ImageFormat.Gif);
return "/nowtime.gif";
}
}
}
///////////////////////////////////////////
<%@page language="C#"%>
<%@Import namespace="Wmj"%>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e)
{
MyTempImage myTempImage=new MyTempImage();
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>
例子很簡單,只要懂得了這個原理,你就可以根據(jù)數(shù)據(jù)庫中的數(shù)據(jù)動態(tài)的繪制曲線圖、比例餅圖、柱狀圖了。
新聞熱點(diǎn)
疑難解答
圖片精選