You should create a class, named
ImageResult
, that extends the
ActionResult
and override the
ExecuteResult
method like in the example below.
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
try
{
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = this.ContentType;
if (this.ImageStream == null)
{
string filePath = context.HttpContext.Server.MapPath("/Content/noimageSmall.jpg");
System.Drawing.Image imageIn = System.Drawing.Image.FromFile(filePath);
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
}
else
{
byte[] buffer = new byte[4096];
while (true)
{
int read = this.ImageStream.Read(buffer, 0, buffer.Length);
if (read == 0)
break;
response.OutputStream.Write(buffer, 0, read);
}
}
response.End();
}
catch (Exception ex)
{
MvcBasicLog.LogException(ex);
}
}
For details (and demo code) about this you could have a look on my next article (sub-chapters ImageResult and OpenFileResult):
MVC Basic Site: Step 3 – Dynamic Layouts and Site Admin with: AJAX, jqGrid, Controller Extensions, HTML Helpers, and more[
^]