How to return an image from a C # asp.net-mvc-2 controller action?

I am creating images with byte[]as below.

public FileContentResult GetEmployeeImage(int empId)
{
   MemoryStream ms = new MemoryStream(byteArray);
   Image returnImage = Image.FromStream(ms);
   return returnImage;//How should i return this image to be consumed by javascript.
}

I want to return this image to the browser through the controller action method, since it can be used by my javascript code and displayed in the browser. How should I do it?

+5
source share
1 answer

You do not need to create an image object; you just want to return the raw data.
The browser will read the raw data into the image.

return File(byteArray, "image/png");

Obviously, you need to pass the correct type of content, depending on which image format is in the byte array.

+7

All Articles