Balaji
Balaji

Reputation: 2189

Display image from IMAGE object in 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.

Upvotes: 5

Views: 3271

Answers (5)

Qing Yu
Qing Yu

Reputation: 21

You can convert the image to 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

Upvotes: 0

Dale Ragan
Dale Ragan

Reputation: 18270

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.

Upvotes: 4

mkchandler
mkchandler

Reputation: 4758

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.

Upvotes: 4

Gregoire
Gregoire

Reputation: 24872

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

Upvotes: 1

Giovanni Galbo
Giovanni Galbo

Reputation: 13081

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"/>.

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

Upvotes: 2

Related Questions