转换后的字节数组图像不显示core.net

时间:2017-12-06 08:54:43

标签: c# asp.net-mvc asp.net-core

在我的详细视图中,为了显示有关(在我的情况下是一个任务)的更多信息,我将显示Image。目前,图像不会显示在视图中。怎么了?

这是我的代码:

控制器中我的ActionResult

public ActionResult ShowImage(int id)
{
    var task = _context.Task.SingleOrDefault(m => m.ID == id);
    Image image = byteArrayToImage(task.Image);
    return View(image);
}

转换器方法:

private Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

我的观点:

<img src="@Url.Action("ShowImage", new { id = Model.ID } )" class="img-responsive"/>

这是返回的打印屏幕。看到图像的输出,它看起来很好。

See the image for the output, it looks fine

1 个答案:

答案 0 :(得分:0)

您应该使用FileContentResult从操作中返回文件。然后你的行动变得如此简单:

private final static PseudoClass GREEN = PseudoClass.getPseudoClass("green");
private final static PseudoClass RED = PseudoClass.getPseudoClass("red");

...

boolean isGreen = clickedBtn.getPseudoClassStates().contains(GREEN);
clickedBtn.pseudoClassStateChanged(GREEN, !isGreen);
clickedBtn.pseudoClassStateChanged(RED, isGreen);

注意:

  1. 您应该在上面适当地设置public IActionResult ShowImage(int id) { var task = _context.Task.SingleOrDefault(m => m.ID == id); return File(task.Image, "image/jpeg"); //TODO: Set Content-Type appropriately } 参数(我已猜测)。
  2. 在上面的代码中,我使用ControllerBase.File方法返回contentType。如果您的控制器未继承FileContentResultController,则需要直接返回ControllerBase