views:

27

answers:

2

I am sending a bitmap to a View in ASP.NET MVC. I have a property in my ViewModel:

public Bitmap TemplateImage { get; set; }

In my View, I want to be able to render that Bitmap image but I can't figure out how to do it.

A: 

One solution would be to create a new action, like this:

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);
}

and send an ID for the image instead and reference it like this:

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

Since HTTP isn't meant to be able to stuff both HTML and binary image data down the same pipeline in the same connection, this makes passing Bitmap data to your View pointless. You have to find another way around by storing (perhaps temporarily) the Bitmap data and having the client request it via a unique URL.

James Dunne