我想要能够使用C#的Web服务创建一个简单的PNG图像,例如一个红色的正方形,然后通过一个<img src="myws.ashx?x=100>
HTML元素调用生成的图像。
一些示例HTML:
<hmtl><body>
<img src="http://mysite.com/webservice/rectangle.ashx?size=100">
</body></html>
有没有人能够拼凑出一个简单(可行的)C#类来帮助我开始?一旦启动,我相信我可以完成它真正要做的事情。
- 最终目标是创建一个简单的红/黄/绿(RAG)嵌入式状态标记,用于显示性能指标等数据驱动的网页*
- 我希望它使用PNG图像,因为我预计将来会使用透明度*
- 请使用ASP.NET 2.0 C#解决方案...(我还没有生产3.5的系统)
tia
解决方案
rectangle.html
<html>
<head></head>
<body>
<img src="rectangle.ashx" height="100" width="200">
</body>
</html>
矩形.ashx
<%@ WebHandler Language="C#" Class="ImageHandler" %>
矩形.cs
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int width = 600; //int.Parse(context.Request.QueryString["width"]);
int height = 400; //int.Parse(context.Request.QueryString["height"]);
Bitmap bitmap = new Bitmap(width,height);
Graphics g = Graphics.FromImage( (Image) bitmap );
g.FillRectangle( Brushes.Red, 0f, 0f, bitmap.Width, bitmap.Height ); // fill the entire bitmap with a red rectangle
MemoryStream mem = new MemoryStream();
bitmap.Save(mem,ImageFormat.Png);
byte[] buffer = mem.ToArray();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(buffer);
context.Response.Flush();
}
public bool IsReusable {
get {return false;}
}
}