如何解决在 C# WinForm 中保存图像时出现的“System.Runtime.InteropServices.ExternalException: 'A generic error returned in GDI+.'”错误?

问题描述 投票:0回答:1

在C# WinForm中保存图像时,遇到以下错误:

System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'

这是经过必要修改的更新后的代码:

string imagePath = Path.Combine(imagesFolder, $"SavedImage_{DateTime.Now.Ticks}.png");
image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);

我尝试更改保存的原因,例如使用位图,但不起作用

c# image winforms save gdi+
1个回答
0
投票
Ensure image is a valid Image object and not disposed before saving.
Check for sufficient disk space on the drive where you're saving the image.
Ensure the application has write permissions to the folder.

You can add this checkings:

    if (image == null)
            {
                throw new ArgumentNullException(nameof(image), "The image cannot be null.");
            }

     if (!Directory.Exists(imagesFolder))
            {
                Directory.CreateDirectory(imagesFolder);
            }

或者,如果您不想确切地看到此错误,您可以添加 try-catch。

© www.soinside.com 2019 - 2024. All rights reserved.