在 C# 中调整图像大小 - 空异常?

Resizing image in C# - null exception?

我正在尝试调整比 x 宽的图像大小以使其适合 Word 文档,但我收到以下错误消息:

An exception of type 'System.ArgumentNullException' occurred in System.Drawing.dll but was not handled in user code

Additional information: Value cannot be 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(..) 方法时发生错误。我调试了代码,resizedImage 的 resizedImage、ms 和 RawFormat 属性 都不是 null。我做错了什么?

这适用于宽度小于或等于 650 的图像。

除非文件来自现有文件(resizedImage 是内存位图,与 image 不同),否则您无法使用 RawFormat 进行保存。

使用特定格式进行保存(例如:ImageFormat.PngImageFormat.Jpeg),或者如果要使用原始文件格式