Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image from IMAGE object in MVC

Tags:

asp.net-mvc

I have an object of type IMAGE which holds image. I wanted to display the image in MVC view along with other controls. The way i can think of is to temporary store image on disk and set src of img control. I am sure there would be better way of doing this.

like image 449
Balaji Avatar asked Sep 04 '09 14:09

Balaji


2 Answers

The easiest way to do this in my opinion would be to return a FileStreamResult from your controller.

public FileResult GetImage()
{
    string path = "c:\images\image.jpg";
    return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}

This is a simple implementation, but gives you a starting point for what you are attempting to do.

like image 144
mkchandler Avatar answered Oct 10 '22 02:10

mkchandler


If you are interested in implementing @Giovanni's answer, then I have some code that may be helpful from a past answer I gave located here. The ImageHandler class is an example of what you would want to implement in Giovanni's case.

like image 30
Dale Ragan Avatar answered Oct 10 '22 02:10

Dale Ragan