Donate

Display Bitmap Image In ASP.NET MVC View From Database With Image Data Type

I encountered this weird problem rendering bitmap image from a database table that a friend of mine develops in his company. The task is to display the image on the mvc view. I thought it was a gif or jpeg or png format. But to my surprise as I rendered the binary data on webpage, the image was a bitmap type.. The code to show the bitmap image is as follows. That is to render the bitmap image into a jpeg format.
C# Code:
public ActionResult ShowCategories()  
{   
 var model = from n in oContext.ApplianceCategories  
    select n;  
 return View(model);        
}  

public void ShowImage(int id)  
{  
 var image = (from m in oContext.ApplianceCategories  
     where m.CategoryID == id  
     select m.Picture).FirstOrDefault();  
 TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));  
 Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(image);  
 bitmap1.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
} 
I got the trick from stack overflow... Cheers!

Comments

  1. The task is to display the image on the mvc view. I thought it was a gif or jpeg or png format.

    ReplyDelete
  2. Showing images on MVC in jpeg or png format is pretty straightforward.
    For .bmp images is a bit tricky.

    ReplyDelete

Post a Comment

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.