Jake Schmitt
Jake Schmitt

Reputation: 11

Display Bitmap in model with Razor

A webApp I am currently working on requires me to store photos as a byte[] in the database. It then needs me to pull those out and display them in a view. I created a photoContainer which holds the bitmap instead of the byte[] and pass that to the view. But I can't seem to figure out how to display it from there. I've tried setting the source to the model.Image and that didn't work. Is there anyway to display a bitmap in my model in the view?

Thanks in advance.

Upvotes: 1

Views: 4771

Answers (1)

Michael Grassman
Michael Grassman

Reputation: 1935

In a controller place this

public FileResult GetImg(int id)
    {                       
         var image = db.Categories.First(m => m.CategoryID == id).Picture;

         byte[] imageData;
         if(image != null)
         {
             imageData = image.ToArray();
             return File( imageData, image.contentType );
         }
         else
         {
             return null;
         }
     }

In your view place this

<img src="@Url.Action("GetImg", "ControllerTheActionAboveIsPlacedIn", new { id = Model.ImageId})" />

Upvotes: 2

Related Questions