【问题标题】:How to serve up image from type System.Drawing.Bitmap (<img> tag?)如何从 System.Drawing.Bitmap 类型(<img> 标签?)
【发布时间】:2011-04-24 02:07:35
【问题描述】:

我正在向 ASP.NET MVC 中的视图发送位图。我的 ViewModel 中有一个属性:

public Bitmap TemplateImage { get; set; }

在我的视图中,我希望能够渲染该位图图像,但我不知道该怎么做。

【问题讨论】:

    标签: asp.net-mvc bitmap system.drawing system.drawing.imaging


    【解决方案1】:

    一种解决方案是创建一个新操作,如下所示:

    public FileContentResult Show(int id)
    {
        var category = northwind.AllCategories().Single(c => c.CategoryID == id);
        byte[] imageByte = category.Picture;
        string contentType = "image/jpeg";
    
        return File(imageByte, contentType);
    }
    

    并改为发送图像的 ID 并像这样引用它:

    <img src="<%: Url.Action("Show","Image",new { id = Model.Category.CategoryID  }) %>
    

    【讨论】:

      【解决方案2】:

      由于 HTTP 并不意味着能够在同一连接中将 HTML 和二进制图像数据填充到同一管道中,这使得将位图数据传递给您的视图毫无意义。您必须通过存储(可能是暂时的)位图数据并让客户端通过唯一的 URL 请求它来找到另一种解决方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-09
        • 2017-02-25
        • 2018-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多