简体   繁体   English

Image.Save崩溃:{“值不能为空。\\ r \\ nParameter name:encoder”}

[英]Image.Save crashing: {“Value cannot be null.\r\nParameter name: encoder”}

I am trying to save an image into a MemoryStream but it is failing under certain conditions. 我试图将图像保存到MemoryStream但在某些条件下失败。

Here is the code: 这是代码:

The following code succeeds: 以下代码成功:

Image img = Bitmap.FromStream(fileStream);
MemoryStream ms = new MemoryStream();
img.Save(ms, img.RawFormat);  // This succeeds.

The following code fails: 以下代码失败:

Image img = Bitmap.FromStream(fileStream);
Image thumb = img.GetThumbnailImage(thumbWidth, thumbHeight, null, System.IntPtr.Zero);

MemoryStream ms = new MemoryStream();
thumb.Save(ms, thumb.RawFormat);  // This fails.

Notice that the second snippet is using an image created using Image.GetThumbnailImage . 请注意,第二个代码段使用的是使用Image.GetThumbnailImage创建的图像。

What is the difference? 有什么不同? Does anyone have any idea why is it failing? 有谁知道它为什么失败?

I believe the problem has to do with this part of the GetThumbnailImage documentation : 我相信这个问题与GetThumbnailImage文档的这一部分有关:

If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. 如果图像包含嵌入的缩略图图像,则此方法将检索嵌入的缩略图并将其缩放到请求的大小。 If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image. 如果图像不包含嵌入的缩略图图像,则此方法通过缩放主图像来创建缩略图图像。

This probably accounts for the intermittent behaviour (AKA "certain conditions"). 这可能是间歇性行为(AKA“某些条件”)的原因。 The explanation is in the following Microsoft Connect ticket : 解释在以下Microsoft Connect票证中

The underlying API is not able to locate an encoder for the MemoryBmp image type. 底层API无法为MemoryBmp图像类型定位编码器。 We will need to investigate this will the GDI+ team. 我们需要对GDI +团队进行调查。 In the meantime, you should be able to simply change your ImageFormat to ImageFormat.Bmp rather than ImageFormat.MemoryBmp and it should work. 在此期间,您应该能够简单地将ImageFormat更改为ImageFormat.Bmp而不是ImageFormat.MemoryBmp,它应该可以工作。 It will still be saved to the MemoryStream using the BMP format. 它仍将使用BMP格式保存到MemoryStream。

In all likelihood, if there is no embedded thumbnail, the new thumbnail generated by the GetThumbnailImage API is in fact going to have a RawFormat of MemoryBmp which has no associated encoder - thus the specific error message you're seeing. 很有可能,如果没有嵌入的缩略图, GetThumbnailImage API生成的新缩略图实际上将具有RawFormatMemoryBmp ,它没有关联的编码器 - 因此您看到的是特定的错误消息。

Just don't use thumb.RawFormat ; 只是不要使用thumb.RawFormat ; since you know it's a bitmap anyway, use ImageFormat.Bmp instead. 既然你知道它是位图,那么请使用ImageFormat.Bmp

PS Please note that although I deleted my earlier answer because it turned out to not to be relevant to this particular issue, it is still important to use the GetThumbnailImage API properly as the documentation specifies; PS请注意,虽然我删除了我之前的答案,因为事实证明它与此特定问题无关,但正如文档所指定的那样,正确使用GetThumbnailImage API仍然很重要; you must pass a valid delegate for the callback parameter instead of null , otherwise it can fail, and you still need to be wrapping disposables in using clauses. 必须callback参数传递一个有效的委托而不是null ,否则它可能会失败,您仍然需要在using子句中包装一次性用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Image.Save() 抛出异常“值不能为空。/r/n 参数名称:编码器” - Image.Save() throws exception "Value cannot be null./r/nParameter name: encoder" 值不能为空。\\ r \\ n参数名称:输入 - Value cannot be null.\r\nParameter name: input “值不能为空。\\r\\n参数名称:文本” - “Value cannot be null.\r\nParameter name: text” {“值不能为空。\\ r \\ nParameter name:s”} - {“Value cannot be null.\r\nParameter name: s”} dc.BeginDialogAsync:“值不能为空。\\ nParameter name:options” - dc.BeginDialogAsync: “Value cannot be null.\nParameter name: options” C#Powershell-Exchange管理{“值不能为空。\\ r \\ n参数名称:serverSettings”} - C# Powershell - Exchange management {“Value cannot be null.\r\nParameter name: serverSettings”} Dynamics Crm 2015:值不能为空。\\ r \\ n参数名称:详细信息 - Dynamics Crm 2015: Value cannot be null.\r\nParameter name: detail “值不能为空。\\ r \\ n参数名称:实体(ASP.NET Web API) - "Value cannot be null.\r\nParameter name: entity (ASP.NET Web API) 根据xml错误{“值不能为空。\\ r \\ n参数名称:元素”}创建对象 - Creating objects from xml error {“Value cannot be null.\r\nParameter name: element”} 值不能为空,参数名称来源 - Value cannot be null, r nparameter name source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM