开发者

Reusing Bitmap variable for Image.Save, GDI+ Error

开发者 https://www.devze.com 2023-01-23 05:46 出处:网络
开发者_运维技巧I have a series of bitmap images that I need to save using .NET (C#) but am running into the generic 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

精彩评论

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

关注公众号