我正在尝试调整图像的大小(如果它比x宽)以使其适合Word文档,但出现以下错误消息:
System.Drawing.dll中发生类型为'System.ArgumentNullException'的异常,但未在用户代码中处理
附加信息:值不能为null。
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Image image = System.Drawing.Image.FromFile(physicalPath);
System.Drawing.Image resizedImage;
if (image.Width > 650)
{
double multiplier = image.Width/650.0;
int newWidth = 650;
int newHeight = (int) (image.Height/multiplier);
resizedImage = (System.Drawing.Image)new Bitmap(image, new Size(newWidth,newHeight));
}
else
{
resizedImage = image;
}
image.Dispose();
resizedImage.Save(ms, resizedImage.RawFormat);
}
执行resizedImage.Save(..)方法时发生错误。我调试了代码,resizeImage或resizedImage的ms或RawFormat属性都不为空。我究竟做错了什么?
对于宽度小于或等于650的图像,这可以正常工作。
RawFormat
除非文件来自现有文件(resizedImage
不同于,它是内存中的位图),否则您无法保存使用image
。
使用特定的格式进行保存(例如:ImageFormat.Png
或ImageFormat.Jpeg
),如果要使用原始文件格式进行保存,请使用image.RawFormat
代替resizedImage.RawFormat
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句