我正在向 ASP.NET MVC 中的视图发送位图。我的 ViewModel 中有一个属性:
public Bitmap TemplateImage { get; set; }
在我的视图中,我希望能够渲染该位图图像,但我不知道该怎么做。
我正在向 ASP.NET MVC 中的视图发送位图。我的 ViewModel 中有一个属性:
public Bitmap TemplateImage { get; set; }
在我的视图中,我希望能够渲染该位图图像,但我不知道该怎么做。
一种解决方案是创建一个新操作,如下所示:
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 }) %>
由于 HTTP 并不意味着能够在同一连接中将 HTML 和二进制图像数据填充到同一管道中,这使得将位图数据传递到您的视图毫无意义。您必须通过存储(可能是暂时的)位图数据并让客户端通过唯一的 URL 请求它来找到另一种方法。