C# 从数组中保存图像
我有一个已调整大小的图像数组,我想做的是直接从数组中保存它们...
foreach (Image I in Resizedimages)
{
string f = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\\NewImages\\" + names[n];
I.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);
n++;
}
问题是每次运行程序时都会收到未处理的异常“GDI+ 中发生一般错误”并且我知道这与 save 方法有关。我假设这个问题以前已经被问过,如果是这样的话,我很抱歉。在网上搜索了几个小时后,我尝试了许多不同的修复方法,我想这可能是我的程序所特有的。有什么想法吗?
I have an array of images I have resized, what I'm trying to do is save them straight from the array...
foreach (Image I in Resizedimages)
{
string f = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\\NewImages\\" + names[n];
I.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);
n++;
}
The problem is EVERY TIME I run the program I get an unhandled exception "A generic error occurred in GDI+" and I know for a fact that it is something to do with the save method. I'm assuming this question has been asked before, and if that is the case then I am sorry. I have tried many different fixes after searching for hours online I thought that maybe it's specific to my program. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是位图对象或图形对象锁定了某些内容。对某些内容调用 Dispose() 可能会起作用,或者您可以复制到新的位图并保存它:
为了避免 GDI 锁定文件,您可以从 MemoryStream 创建图像:
The problem may be with something being locked, either by the Bitmap object or the Graphics object. Calling
Dispose()
on something might work or you can just copy to a new bitmap and save that:To avoid file-locking by GDI, you can create an image from a MemoryStream: