简体   繁体   English

Image.Save() 抛出异常“值不能为空。/r/n 参数名称:编码器”

[英]Image.Save() throws exception "Value cannot be null./r/nParameter name: encoder"

I am getting "Value cannot be null.\\r\\nParameter name: encoder" error while saving a Bitmap image using RawFormat.使用 RawFormat 保存位图图像时出现“值不能为空。\\r\\n参数名称:编码器”错误。 Sample code:示例代码:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var image = new System.Drawing.Bitmap(500, 400);
            var stream = new MemoryStream();
            image.Save(stream, image.RawFormat);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.ToString());
        }
    }
}

The RawFormat doesn't exist in the existing list of ImageEncoders as below code returns null. RawFormat 在现有的 ImageEncoders 列表中不存在,因为下面的代码返回 null。

var imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(codec => codec.FormatID == image.RawFormat.Guid);

Note: The image could be any type(JPEG, BMP, PNG) etc. Image.Save() should work on image.RawFormat.注意:图像可以是任何类型(JPEG、BMP、PNG)等。Image.Save() 应该适用于 image.RawFormat。 RawFormat is not Bitmap type. RawFormat 不是位图类型。 If I Change image.RawFormat to ImageFormat.Bmp, the save operation succeeds.如果我将 image.RawFormat 更改为 ImageFormat.Bmp,则保存操作成功。

Referred below links but found nothing for making it independent of image type.参考下面的链接,但没有发现使其独立于图像类型。

Image.Save crashing: {"Value cannot be null.\\r\\nParameter name: encoder"} Why is Image.Save(Stream, ImageFormat) throwing an exception? Image.Save 崩溃:{“值不能为空。\\r\\n参数名称:编码器”} 为什么 Image.Save(Stream, ImageFormat) 会抛出异常?

Any suggestions are welcome.欢迎任何建议。

If you load an image from disk, you can use image.RawFormat to save that image using its original format.如果从磁盘加载图像,则可以使用image.RawFormat以原始格式保存该图像。 However there is no encoder associated with an in-memory bitmap (which is what you are creating in this sample application), so you'll have to specify an image format yourself (ie. ImageFormat.Bmp ).但是,没有与内存中位图(这是您在此示例应用程序中创建的)相关联的编码器,因此您必须自己指定图像格式(即ImageFormat.Bmp )。

您可以使用它,它将被修复:

image.Save(stream,ImageFormat.Jpeg);

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

相关问题 Image.Save崩溃:{“值不能为空。\\ r \\ nParameter name:encoder”} - Image.Save crashing: {“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