JD.
JD.

Reputation: 15541

How do I get a bitmap image to show in MVC 2

I am pretty new to MVC 2 and would be grateful for any help.

In my database I have a field for the thumbnail which is stored as a System.Drawing.BitMap.

I have a partial view that needs to generate the image in the html.

I have seen links to FileResult but this is the controller. With model binding how do I embed the image in the generated html page from a partial view?

Not sure if I need some "image" tag in my html or what format the data from the partial view must be in for it to show the thumbnail?

JD

Upvotes: 2

Views: 3503

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You need to have a controller action which returns a FileStreamResult and then use an <img> tag pointing to this controller action.

public ActionResult Image(int id)
{
    byte[] imageData = GetImageFromDb(id);
    return File(imageData, "image/jpeg");
}

And then inside your view:

<img src="<%: Url.Action("image", new { id = Model.ImageId }) %>" alt="some image" />

Upvotes: 8

Related Questions