Displaying database image (bytes[]) in Razor/MVC3? Displaying database image (bytes[]) in Razor/MVC3? arrays arrays

Displaying database image (bytes[]) in Razor/MVC3?


Model.Content is a byte[] image.

@{    string imageBase64 = Convert.ToBase64String(Model.Content);    string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);}

Used like so:

<img src="@imageSrc" alt="@Model.Name" width="100" height="100" />


Eventually, I went for the two trips route. Highly inefficient, as the image bytes were already ready to go on the view. There should be a web helper image control that will render the image directly from the image's byte array (or image type). Maybe there is and I missed it.

To get the bytes (yes, all over again):

    [AcceptVerbs(HttpVerbs.Get)]    public ActionResult GetThumbnailImage(string itemListID)    {        byte[] imageBytes = null;        client = new FeederServiceClient();        imageBytes = client.GetItemThumbnail( itemListID );        if (imageBytes == null)        {            return new FilePathResult("~/Content/Images/NoPhoto.png", "image/png");        }        else        {            return new FileContentResult(imageBytes, "image/jpeg");        }    }

Then to display the image:

<img src="@Url.Content("~/Thumbnails/" + @Model.dsResults.Tables[0].Rows[i]["ItemListID"] )" />

With the following route placed in Global.asax:

routes.MapRoute(name: "Thumbnails", url: "Thumbnails/{itemListID}", defaults: new { controller = "Results", action = "GetThumbnailImage" });


I don't think having a byte array in your View will help you. You'll need to use a controller action as the img src as described in your linked answer.

[Authorize][AcceptVerbs(HttpVerbs.Get)]public ActionResult Image(int id){    byte[] byteArray = _imageRepository.GetImage(id);    return new FileContentResult(byteArray, "image/jpeg");}<img src="Image/1234" alt="Image 1234"/>