开发者

Reusing Bitmap variable for Image.Save, GDI+ Error

开发者_运维技巧

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜