C# Saving Images From Array
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 Resizedi开发者_运维知识库mages)
{
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?
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:
Bitmap copy = new Bitmap(I);
copy.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);
To avoid file-locking by GDI, you can create an image from a MemoryStream:
MemoryStream stream = new MemoryStream(File.ReadAllBytes(fileName));
Image image = Image.FromStream(stream);
精彩评论