Posts

Showing posts with the label ASP.NET MVC Image

Donate

Display Images Using Generic Handler In ASP.NET MVC

Image
There are several options on how to show images in ASP.NET MVC. One of them is using a generic handler. This method was applied since the early days of ASP.NET Webforms and still can be used with recent frameworks. The class below will render the images on the page by calling the ProcessRequest method which will then get the image from datasources such as database and file system and return it as a Bitmap object. public class ImageHandler : IHttpHandler { private OnlineShoppingDBEntities shoppingContext; public ImageHandler () { shoppingContext = new OnlineShoppingDBEntities(); } public void ProcessRequest (HttpContext context) { if (context.Request.QueryString[ "ProductID" ] != null ) { Product product = shoppingContext.Products.Find(Convert.ToInt32(context.Request.QueryString[ "ProductID" ])); if (product != null ) { Byte[] bytes = product.Image; int height = 0 ; int width = 0 ; height = 100 ; width = 10

Display Images in ASP.NET MVC Using Custom HtmlHelper

Image
Here's a simple Image Helper for showing known image types. I've tested this for old format such as bitmaps and images with OLE headers. This helper sets the image width and height with fixed values. You may change it dynamically or update this helper to accept parameters for width and height. C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /// <summary> /// Helper for showing images /// </summary> public static MvcHtmlString ImageLink( this HtmlHelper htmlHelper, byte [] photo, string id) { string imageSrc = null ; if (photo != null ) { using(MemoryStream ms = new MemoryStream()) { string imageBase64 = string .Empty; Image xImage = (Bitmap)(( new ImageConverter()).ConvertFrom(photo)); if (ImageFormat.Bmp.Equals(xImage.RawFormat)) { // strip out 78 byte OLE header (don't need to do this for normal images) ms.Write(photo, 78, photo.Length - 78); } else { ms.Write(photo, 0, photo.Length);

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 o

Donate