我最近了解到需要处理GDI对象,因为GC不像C#中的其他对象那样对它们进行处理。我有一些位图,希望它们在表单的生命周期内可用,但我对一些事情不确定...
When recreating a bitmap object, do I need to dispose of the original object first? I'm thinking not, but I thought I'd check. For example:
// Global Bitmap bmp; // In form constructor... bmp = new Bitmap(source); // In a function... if(bmp != null) { bmp.Dispose bmp = null } bmp = new Bitmap(source2); // On paint (if bmp not null)... DrawImage(bmp, rectangle);
Because I want to keep bitmaps for the lifetime of the form, can I simply dispose of them on the form close event?
Is there a better alternative than preserving the bitmaps? Creating each bitmap from a file and disposing on paint performs too slowly. Using Image instead of Bitmap lowers the image quality.
非常感谢您!
.Dispose()
。无论是GDI对象还是任何其他.NET对象,您都必须明确调用.Dispose()
。 - Enigmativity