Add Bitmap to HTML

use*_*662 4 html c# asp.net

我有一个ASP.NET Web表单.此Web表单具有一个生成一些HTML的事件处理程序.此HTML基于时间,这就是它在事件处理程序中创建的原因.根据一天中的时间,通过以下方法以编程方式创建图像:

private Bitmap GetImageForTime(DateTime time)
{
  Bitmap bitmap = new Bitmap();

  // Dynamically build the bitmap...

  return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

我想在生成HTML时调用此方法.但是,我不想在服务器上写图像.相反,我想找出一种方法将其与HTML一起写出来.从某种意义上说,我正在努力实现以下目标:

protected void myLiteral_Load(object sender, EventArgs e)
{
  string html = "<table><tr><td>";
  html += GetImageForTime(DateTime.Now);  // This is the problem because it's binary.
  html += "</td><td>";
  html += GetWeatherHtmlText();
  html += "</td></tr></table>";
  myLiteral.Text = html;
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,怎么样?

Tob*_*oby 5

我建议实现一个生成图像的IHttpHandler并将其作为字节流返回.然后在页面上的标记中,将src属性设置为HTTP Handler的地址.

<html><body><img src="TimeImageHandler.ashx"/></body></html>
Run Code Online (Sandbox Code Playgroud)

示例:http://www.c-sharpcorner.com/uploadfile/desaijm/httphandlersforimages11152005062705am/httphandlersforimages.aspx

一旦你意识到它们,通用HTTP处理程序就很容易创建:

public class TimeImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        Bitmap bitmap = GetImageForTime(DateTime.Now);
        context.Response.ContentType = "image/jpeg";
        bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

    public bool IsReusable { get { return false; } }
}
Run Code Online (Sandbox Code Playgroud)