mercredi 25 février 2015

ASP.NET MVC Image in response output stream. Not working

I want to display an image directly into the view and so I am calling an action method and writing the image in output stream.


But I am getting error saying "The image cannot be displayed as it contains error". I am sure the image is generating properly but not sure whats wrong with my controller code.. had anyone came across such scenario?


Thanks in advance..


Here is my controller action



[ChildActionOnly]
public ActionResult GetCaptcha()
{
try
{

this.Session["CaptchaImageText"] = GenerateRandomCode();
// Create a CAPTCHA image using the text stored in the Session object.
RandomImage ci = new RandomImage(this.Session
["CaptchaImageText"].ToString(), 200, 60);
// Change the response headers to output a JPEG image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
// Write the image to the response stream in JPEG format.

using (MemoryStream ms = new MemoryStream())
{
ci.Image.Save(ms, ImageFormat.Jpeg);
ms.WriteTo(this.Response.OutputStream);
}

// ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
// Dispose of the CAPTCHA image object.
ci.Dispose();
return new EmptyResult();

}
catch (Exception ex)
{
ex = null;
return null;
}

}


And here is how I am calling the action method from view



<tr><td> <iframe> @{ Html.Action("GetCaptcha"); } </iframe> </td></tr>

Aucun commentaire:

Enregistrer un commentaire