为什么 Image.Save(Stream, ImageFormat) 会抛出异常?

发布于 2024-10-02 10:03:22 字数 643 浏览 0 评论 0原文

我正在尝试将图像转换为图标。我的功能是:

private Icon GenerateIcon(int width, int height)
{
    using (Bitmap icon = _backingImage.GetThumbnailImage(width, height, () => false, System.IntPtr.Zero) as Bitmap)
    using(MemoryStream imgStream = new MemoryStream())
    {
        icon.Save(imgStream, System.Drawing.Imaging.ImageFormat.Icon);
        return new Icon(imgStream);
    }
}

但是当程序调用该方法时,它会在我调用 icon.Save 的地方抛出一个 ArgumentNullException("encoder")

我觉得这很奇怪,因为我没有传入编码器,我希望框架找出编码器应该是什么,这就是我传入 ImageFormat 的原因。

是否没有 ImageFormat.Icon 的编码器,或者我做错了什么?

I am trying to convert an image to an icon. My function is:

private Icon GenerateIcon(int width, int height)
{
    using (Bitmap icon = _backingImage.GetThumbnailImage(width, height, () => false, System.IntPtr.Zero) as Bitmap)
    using(MemoryStream imgStream = new MemoryStream())
    {
        icon.Save(imgStream, System.Drawing.Imaging.ImageFormat.Icon);
        return new Icon(imgStream);
    }
}

But when the programme calls the method, it throws an ArgumentNullException("encoder") where I'm calling icon.Save.

I find this odd because I'm not passing in an encoder, I want the framework to figure out what the encoder should be, which is why I'm passing in an ImageFormat.

Is it that there aren't any encoders for ImageFormat.Icon, or is there something I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-10-09 10:03:22

您猜对了:GDI+ 仅支持 ICON解码器

您可能想自己执行转换。在这种情况下,请参阅 http://www.codeproject.com/KB/GDI- plus/safeicon.aspx

You guessed it right: GDI+ only supports an ICON decoder.

You might want to perform the conversion yourself. In that case, see http://www.codeproject.com/KB/GDI-plus/safeicon.aspx.

撞了怀 2024-10-09 10:03:22

只需将图像转换为图标:

Icon myIcon = Icon.FromHandle(((Bitmap)myImage).GetHicon())

然后使用流保存:

myIcon.Save(myStream);

问候,
凯特

Just convert image to icon:

Icon myIcon = Icon.FromHandle(((Bitmap)myImage).GetHicon())

and then save it using stream:

myIcon.Save(myStream);

regards,
Kate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文