简体   繁体   中英

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

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM