1

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();

            this.Response.Clear();

            this.Response.ContentType = "image/jpeg";

            Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));

            Graphics graphicImage = Graphics.FromImage(img);
            graphicImage.SmoothingMode = SmoothingMode.AntiAlias;

            graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
                                    new Font("Arial", 12, FontStyle.Bold),
                                    SystemBrushes.WindowText, new Point(100, 250));

            graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);

            //Save the new image to the response output stream.
            img.Save(this.Response.OutputStream, ImageFormat.Jpeg); 

            // Create a CAPTCHA image using the text stored in the Session object.

            graphicImage.Dispose();
            img.Dispose();
        }
        catch (Exception ex)
        {
            ex = null;
        }
        var v = new FileStreamResult(Response.OutputStream,"image/jpeg");
        return v;
    }

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

    <tr><td> <iframe width="200px" height="80px" frameborder="1" scrolling="no"> <img src="@Url.Action("GetCaptcha")" alt="SimpleChart" /> </iframe>  </td></tr>
4
  • 1
    instead of returning EmptyResult() from controller action return File() and instead of using @Html.Action() inside view use @Url.Action() Commented Feb 26, 2015 at 5:34
  • Thanks for quick response. I tried with your suggestion but no luck.. code var fileresult = new FileStreamResult(this.Response.OutputStream, "image/jpeg"); return fileresult; Commented Feb 26, 2015 at 5:49
  • instead of rendering image inside iframe use this <img src='@Url.Action("GetCaptcha")' /> Commented Feb 26, 2015 at 5:53
  • Updated my code with the changes.. no errors now but image is not loading Commented Feb 26, 2015 at 7:17

2 Answers 2

2

You need to return file instead of EmptyResult.

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

and in the view side:

<tr><td> <iframe>  <img src='@Url.Action("GetCaptcha")'/> </iframe>  </td></tr>

Update

Don't write to response instead do like below.

[ChildActionOnly]
public ActionResult GetCaptcha()
{

    try
    {
        byte[] imageByteData = null
        this.Session["CaptchaImageText"] = GenerateRandomCode();

        this.Response.Clear();

        this.Response.ContentType = "image/jpeg";

        Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));

        Graphics graphicImage = Graphics.FromImage(img);
        graphicImage.SmoothingMode = SmoothingMode.AntiAlias;

        graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
                                new Font("Arial", 12, FontStyle.Bold),
                                SystemBrushes.WindowText, new Point(100, 250));

        graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);

        MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Jpeg);
            imageByteData = stream.ToArray();

        // Create a CAPTCHA image using the text stored in the Session object.

        graphicImage.Dispose();
        img.Dispose();
        return File(imageByteData, "image/png"); 
    }
    catch (Exception ex)
    {
        ex = null;
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

The error is gone when I use your code <img src='@Url.Action("GetCaptcha")'/> but the image is not getting loaded in the page
Inspect the element for the image's source and also update your question with update code.
Updated my code with the changes.. no errors now but image is not loading
+1 thanks for the quick answers @JenishRabadiya the image is loading now... one more thing I did is to remove the [ChildActionOnly] from my action method and the image is loading! Appreciate ur time..
0

I think you are using FileStreamResult wrong.

Change img.Save(...) to same into a memory stream and in FileStreamResult(...) return that memory stream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.