I have a series of bitmap images that I need to save using .NET (C#) but am running into the generic GDI+ error.
I am trying to reuse the same variable which may be my problem.
For example:
Bitmap pic = MethodThatReturnsBitmap();
pic.Save(MyPath);
pic = AnotherMethodThatReturnsBitmap();
pic.Save(AnotherPath);
Do I need to introduce unique variables and/or dispose between each .Save()
?
GDI+ is really finicky about resource management. I have found that, when in doubt, always, always .Dispose()
when you have finished a set of operations with a Bitmap. So, the simple answer is, yes, I think you need to Dispose(). I would go even further and put both Bitmaps in using
statements.
using(Bitmap pic = MethodThatReturnsBitmap())
{
pic.Save(Path);
}
using(Bitmap pic = AnotherMethodThatReturnsBitmap())
{
pic.Save(AnotherPath);
}
精彩评论