3

I am attempting to convert an existing WinForms application to a MVC Web Application.

I am attempting to display an image from a controller.

At the moment I get a System.Drawing.Image back from a function and I am trying to work out how to supply this to the View.

This is my controller:

public System.Drawing.Image getStudentImage(string PersonID)
{
        System.Drawing.Image studentImage = ClientFunctions.GetStudentImage(GlobalVariables.networkstuff, GlobalVariables.testAuth, PersonID);
        return studentImage;
}

And then, in my View I would like to display this image...

Hopefully, like this:

<img id="@pupil.PupilInfo.PersonID" src="~/TakeRegister/getStudentImage/@pupil.PupilInfo.PersonID" alt="" />

When I run the code I get broken images in the browser and when I look at the "src" of the image it looks like this:

http://localhost:19340/TakeRegister/getStudentImage/3058

I get the feeling I am doing more than 1 thing wrong here... (?)

Can anyone give me any pointers please?

Niklas
  • 13,005
  • 23
  • 79
  • 119
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • http://bit.ly/1vvAdl7 – L-Four Nov 21 '14 at 12:25
  • 1
    possible duplicate of [Best way to render System.Drawing.Image in ASP.NET MVC 3 View](http://stackoverflow.com/questions/11546865/best-way-to-render-system-drawing-image-in-asp-net-mvc-3-view) – Ant P Nov 21 '14 at 12:26
  • @AntP thanks! - works perfectly... i was obviously phrasing my question incorrectly in google as i didn't find that :( – Trevor Daniel Nov 21 '14 at 12:40

1 Answers1

7

You can use return File() in your action.

public ActionResult Image(int PersonID)
        {
            System.Drawing.Image image = ClientFunctions.GetStudentImage(GlobalVariables.networkstuff, GlobalVariables.testAuth, PersonID);

            using (var ms = new MemoryStream())
            {
                image.Save(ms, ImageFormat.Jpeg);

                return File(ms.ToArray(), "image/jpeg");
            }
        }
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50