Display image from IMAGE object in MVC

I have an object of type IMAGE that contains an image. I wanted to display the image in the MVC view along with other controls. The way I can think of is to temporarily store the image on disk and install the img control src. I am sure there will be a better way to do this.

+5
asp.net-mvc
source share
5 answers

You can write a handler for streaming image processing, and then refer to the tape drive in your image tag.

For example, you have http: //myapp/media.ashx? ImageId = 10 image stream. You link to your page like this: <img src="http://myapp/media.ashx?imageId=10"/> .

Thus, you do not need to temporarily burn to disk.

+2
source share

The easiest way to do this, in my opinion, is 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 trying to do.

+4
source share

If you are interested in implementing @Giovanni's answer, then I have some code that can be useful from the last answer I gave, located. The ImageHandler class is an example of what you would like to implement in the case of Giovanni.

+4
source share

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

+1
source share

You can convert the image to a Base64 string and assign it to the src attribute of the img tag.

 <img alt="Embedded Image" width="168" height="32" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKgA..." /> 

http://www.greywyvern.com/code/php/binary2base64

0
source share

All Articles