tags:

views:

237

answers:

4

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.

+2  A: 

You can write a handler to stream images out and then reference the streamer in your image tag.

For instance, you have http://myapp/media.ashx?imageId=10 stream out the image. In your page you reference like so: <img src="http://myapp/media.ashx?imageId=10"/&gt;.

This way you don't have to temporarily write to disk.

Giovanni Galbo
+1  A: 

You can serve your image as the response content of a controller action. this response will have the image type as content type.

Gregoire
+3  A: 

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.

mc2thaH
+3  A: 

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.

Dale Ragan