Question

I'm new to asp.net mvc, but I'm trying to do an funny app with GDI+ and I need to do some image view with asp.net mvc.

I have a Model which has an image property:

namespace DomainModel.Entities
{
    public class BackgroundImage
    {
        // Properties
        public Image Image {get; private set; }

        public BackgroundImage()
        {
            Image = Image.FromFile(@"D:\ProjectZero\DomainModel\image\bkg.PNG");

        }
    }
}

The controller sends to the view the following:

public ActionResult Index()
        {

            BackgroundImage bkImg = new BackgroundImage(); 
            return View(bkImg);
        }

The view looks like this:

<p> Height: </p><%= Model.Image.Height //it prints the height of the image %> </br>
<p> Width: </p><%= Model.Image.Width //it prints the width %> </br>
<p> Image: </p><%= Model.Image //does not work to display the image %>

How do I display that image property?

I need that background image for a div. I do not need to resize the image, I only need to display it in it's normal size.

Is there a Html Helper that I miss?

Thank you, for your time!

Was it helpful?

Solution

You need to write a controller action that will write the image into the response stream and set the proper content type to say "image/png". Then you can use the img tag to reference this action in your view:

public ActionResult Image()
{
    byte[] image = GenerateImage();
    return File(image, "image/png");
}

And inside your view:

<img src="<%= Url.Action("Image") %>" alt="" />

OTHER TIPS

You will need a HttpHandler if you want to do things like this.

http://dotnetperls.com/ashx-handler

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top