我尝试将inputstream转换为Image
我尝试使用以下代码。
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
我需要将img传递给mvc视图
@foreach (var img in imageSet)
{
<div class="thumbnail thumbnail-inline">
<label class="checkbox-inline">
<input type="checkbox" onchange="DisplayedImageOnchange(this)" value="@img.Id" />
<img src="@img.Url" class="img-thumbnail" />
</label>
<div class="btn-block">
<input type="file" class="replace file-loading" />
<button class="btn btn-xs btn-danger delete">delete</button>
</div>
</div>
}
public JsonResult Upload(object model)
{
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
return Json(new AdCreative { Id = 100, Url = Url.Content("data:image;base64,@System.Convert.ToBase64String('" + img + "')") }, JsonRequestBehavior.AllowGet);
}
它会抛出以下错误:
来自亚马逊s3响应的上述输入流。
答案 0 :(得分:0)
首先,您可以点击“查看详细信息”来查找更多信息。在异常窗口中,展开任何InnerException
属性。
您是否检查过imagePathErrorMessage.StreamObject
是非空的并提供了一种可接受格式的图像?
这些是JPG,BMP,TIF,GIF和PNG。如果您的图片包含透明度数据,则应首先将其转换为PNG。您可能不会使用RAW数据,因为我认为您需要BitmapImage
而不是Image
如果流有效,则在开始读取之前还需要位于0位置,例如:
imagePathErrorMessage.StreamObject.Position = 0;
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
编辑OP编辑:
在转换之后,是否需要将ResponseStream设置回零,但之前将复制到destination
?
(如果在屏幕截图中显示的断点处,您在destination
上方悬停,是否会显示长度为61153且CurrentPosition为0的流?)
第二次编辑:
这篇帖子Loading an image from a stream without keeping the stream open描述了为什么在使用Image.FromStream时,流需要在图像的生命周期内保持打开状态。