Search code examples
asp.net-mvcbitmapsystem.drawingsystem.drawing.imaging

How to serve up image from type System.Drawing.Bitmap (<img> tag?)


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.


Solution

  • 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  }) %>