I have a view which contains a users id and an image column.
Here\'s what i\'ve tried doing to retrieve the image but i keep getting a box with an red x instead of t
Look at this question I had from a while back - the solution was special ActionResult type for images
Edit: Here's my code. I'm actually creating an ImageResult class from an Image that I created with GDI+ like this :
return new ImageResult()
{
ImageFormat = spriteInfo.ImageFormat,
EncodedImageBytes = spriteInfo.GetImageStream()
};
The image result class is. You'll notice if I provide an EncodedImageBytes parameter it will send that to the output stream. This looks like exactly what you want. On the other hand if you're just passing in an Image then it will just write that Image out to the output stream.
public class ImageResult : ActionResult
{
public ImageResult() { }
public int? Quality { get; set; }
public Image Image { get; set; }
public ImageFormat ImageFormat { get; set; }
public byte[] EncodedImageBytes { get; set; }
public override void ExecuteResult(ControllerContext context)
{
// verify properties
if (EncodedImageBytes == null)
{
if (Image == null)
{
throw new ArgumentNullException("Image");
}
}
if (ImageFormat == null)
{
throw new ArgumentNullException("ImageFormat");
}
// output
context.HttpContext.Response.Clear();
if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
// output stream
Stream outputStream = context.HttpContext.Response.OutputStream;
if (EncodedImageBytes != null)
{
outputStream.Write(EncodedImageBytes, 0, EncodedImageBytes.Length);
}
else
{
ImageUtil.SaveImageToStream(outputStream, Image, ImageFormat, Quality);
}
}
}