为什么 Image.Save(Stream, ImageFormat) 会抛出异常?
我正在尝试将图像转换为图标。我的功能是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您猜对了: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.
只需将图像转换为图标:
然后使用流保存:
问候,
凯特
Just convert image to icon:
and then save it using stream:
regards,
Kate