C# 尝试使用 Image.Save 时“GDI+ 中发生一般错误”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14866603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"A generic error occurred in GDI+" when attempting to use Image.Save
提问by Magnum
I am developing an Outlook 2010 Add-In, and am loading an image from a serialized XML file. The image loads fine, and am able to assign it to a pictureBox object on a Winform no problem. The object is saved in
我正在开发 Outlook 2010 加载项,并且正在从序列化的 XML 文件加载图像。图像加载正常,并且能够将其分配给 Winform 上的图片框对象没问题。对象保存在
[XmlIgnore]
public Bitmap Image
{
get { return this.templateImage; }
set { this.templateImage = value; }
}
When, I attempt to save the physical file onto the harddisk however, I am doing:
但是,当我尝试将物理文件保存到硬盘上时,我正在执行以下操作:
string filePath = Path.Combine(dirPath, item.Id + ".jpg");
try
{
item.Image.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
}
and am getting an A generic error occurred in GDI+. I've checked the write permissions on the folder, and it does have write permissions. I'm unsure what is wrong here. I've also changed the ImageFormat to bmp and png and so forth to see if it was a conversion problem... but it isn't. Would anybody suggest something to try?
并且在 GDI+ 中出现一个通用错误。我检查了文件夹的写权限,它确实有写权限。我不确定这里有什么问题。我还将 ImageFormat 更改为 bmp 和 png 等,以查看它是否是转换问题......但它不是。有人会建议尝试什么吗?
采纳答案by Magnum
Thank you to Simon Whitehead for answering this in the comments. He said, "3) Make sure the file is not in use by anything else (including your code)."
感谢 Simon Whitehead 在评论中回答这个问题。他说,“3)确保文件没有被其他任何东西(包括你的代码)使用。”
So the problem was that my own code was using the item.Image object, and was preventing GDI+ to call the dispose() method on it. The solution was to copy the object into a new object, then use that object to "Write." The resulting code is as follows:
所以问题是我自己的代码正在使用 item.Image 对象,并且阻止 GDI+ 调用它的 dispose() 方法。解决方案是将对象复制到一个新对象中,然后使用该对象“写入”。结果代码如下:
try
{
using (Bitmap tempImage = new Bitmap(item.Image))
{
tempImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (Exception e)
{
Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
}
回答by Vikram Singh Saini
I too faced same error for code line:
我也面临同样的代码行错误:
wmImg.Save(BrandImgPath,ImageFormat.Png);
BrandImgPath = "D:/XYZ/fileName;
Found cause:
找到原因:
XYZ folder didn't exist in D: drive. So my code was creating this folder later. One should ensure if that path exist or not.
D: 驱动器中不存在 XYZ 文件夹。所以我的代码稍后创建了这个文件夹。人们应该确保该路径是否存在。
if (Directory.Exists(@"D:/XYZ")) return;
Hope it will help someone to solve his code mistakes.
希望它能帮助某人解决他的代码错误。
回答by Shadmehr Vadoodi
1. Make Sure That your destination folder have read/write permission (check it twice!).
2. Using Server.MapPath is better
3. Make Sure you have free space on your destination drive or folder.
4. Most of the times we cant user Memory Streamers On Shared Servers, So we should be make sure that provider allow us to use it.
Hope Microsoft Give Detailed Exception Errors instead of "Generic GDI+ Errror" !!!
1. 确保您的目标文件夹具有读/写权限(检查两次!)。
2. 使用 Server.MapPath 更好
3. 确保目标驱动器或文件夹上有可用空间。4. 大多数时候我们不能在共享服务器上使用内存流送器,所以我们应该确保提供者允许我们使用它。
希望微软给出详细的异常错误而不是“通用 GDI+ 错误”!!!
回答by gaffleck
Had this issue myself, needed to check that the folder existed, GDI didn't tell me what went wrong, would have been nice.
我自己有这个问题,需要检查文件夹是否存在,GDI 没有告诉我出了什么问题,本来很好。
回答by Artur K?dzior
In my case it was a spelling mistake in the path to the directory where I was saving the image:
就我而言,这是我保存图像的目录路径中的拼写错误:
if (Directory.Exists(directory_path))
{
image.SaveAs(directory_path + filename);
}
As gaffleck said it would be nice if GDI+ had thrown more informative exception.
正如 gaffleck 所说,如果 GDI+ 抛出更多信息异常,那就太好了。
回答by smoothumut
I had the same generic exception, but then I gave write permission to the IIS on parallel plesk file manager. if you are on windows server, make sure to give write permission to IIS user which was IIS_IUSR in my server
我有相同的通用异常,但后来我在并行 plesk 文件管理器上授予了 IIS 的写权限。如果您在 Windows 服务器上,请确保向我的服务器中的 IIS_IUSR IIS 用户授予写权限
also make sure that the folder you are trying to save is correct as mentioned in above comments
还要确保您尝试保存的文件夹是正确的,如上述评论中所述