Image.Save()抛出异常"值不能null./r/nParameter名称:编码器" [英] Image.Save() throws exception "Value cannot be null./r/nParameter name: encoder"

查看:2876
本文介绍了Image.Save()抛出异常"值不能null./r/nParameter名称:编码器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到编码器值不能null.\r\\\
Parameter名。
示例代码:

I am getting "Value cannot be null.\r\nParameter name: encoder" error while saving a Bitmap image using RawFormat. 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());
        }
    }
}



RawFormat不存在在ImageEncoders现有列表,下面的代码返回null

The RawFormat doesn't exist in the existing list of ImageEncoders as below code returns null.

VAR imageCodecInfo = ImageCodecInfo.GetImageEncoders()FirstOrDefault(编解码器=方式>。编解码器。 FormatID == image.RawFormat.Guid);

注:图像可以是任何类型(JPEG,BMP,PNG)等图像.Save()应该image.RawFormat工作。
RawFormat不是位图的类型。如果我改变image.RawFormat到ImageFormat.Bmp,保存操作成功。

Note: The image could be any type(JPEG, BMP, PNG) etc. Image.Save() should work on image.RawFormat. RawFormat is not Bitmap type. If I Change image.RawFormat to ImageFormat.Bmp, the save operation succeeds.

简称下面的链接,但一无所获为使其成为一个独立的图像类型。

Referred below links but found nothing for making it independent of image type.

Image.Save崩溃:{"值不能null.\r\\\
Parameter名称:编码器"}

为什么Image.Save(流,的imageformat)抛出一个异常?

任何建议都欢迎。

推荐答案

如果你从磁​​盘加载图像,你可以使用 image.RawFormat 利用其原有的格式保存该图像。但是没有与内存中的位图相关的编码器(这是你在此示例应用程序创建),所以你必须指定自己(一种图像格式即 ImageFormat.Bmp )。

If you load an image from disk, you can use image.RawFormat to save that image using its original format. 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).

这篇关于Image.Save()抛出异常"值不能null./r/nParameter名称:编码器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆