【问题标题】:ASP.NET MVC Image in response output stream. Not working响应输出流中的 ASP.NET MVC 图像。不工作
【发布时间】:2015-04-28 09:33:16
【问题描述】:

我想将图像直接显示到视图中,因此我正在调用一个操作方法并将图像写入输出流。

但我收到错误消息,提示“图像包含错误,无法显示”。我确定图像生成正确但不确定我的控制器代码有什么问题..有人遇到过这种情况吗?

提前谢谢..

这是我的控制器操作

[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;
    }

这是我从视图中调用操作方法的方式

    <tr><td> <iframe width="200px" height="80px" frameborder="1" scrolling="no"> <img src="@Url.Action("GetCaptcha")" alt="SimpleChart" /> </iframe>  </td></tr>

【问题讨论】:

  • 而不是从控制器操作返回EmptyResult(),而是返回File(),而不是使用@Html.Action()内部视图使用@Url.Action()
  • 感谢您的快速回复。我尝试了您的建议,但没有运气.. code var fileresult = new FileStreamResult(this.Response.OutputStream, "image/jpeg");返回文件结果;
  • 使用 &lt;img src='@Url.Action("GetCaptcha")' /&gt; 而不是在 iframe 中渲染图像
  • 用更改更新了我的代码。现在没有错误,但图像没有加载

标签: asp.net-mvc image response outputstream


【解决方案1】:

您需要返回文件而不是EmptyResult

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

在视图方面:

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

更新

不要写回复,而是像下面这样。

[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;
    }

}

【讨论】:

  • 当我使用你的代码时错误消失了 但是页面中没有加载图像
  • 检查图像源的元素,并使用更新代码更新您的问题。
  • 用更改更新了我的代码。现在没有错误,但图像没有加载
  • +1 感谢@JenishRabadiya 的快速回答,现在正在加载图像......我做的另一件事是从我的操作方法中删除 [ChildActionOnly] 并且图像正在加载!感谢你的时间..
【解决方案2】:

我认为您使用 FileStreamResult 错误。

将 img.Save(...) 更改为相同的内存流并在 FileStreamResult(...) 中返回该内存流。

【讨论】:

    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2012-09-06
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多