When to delete GDI objects, in particular, bitmaps

I recently learned about the need to dispose of GDI objects because the GC does not care about them like other objects in C #. I have bitmaps that I would like to get throughout my life forms, but I'm not sure about some things ...

  • When recreating a bitmap object, do I need to delete the original object first? I do not think, but I thought I would check. For instance:

    // 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);
    
  • Since I want to save bitmaps for the life time of the form, can I just get rid of them in the form closing event?

  • Is there a better alternative than saving raster images? Creating each bitmap from a file and removing paint is too slow. Using Image instead of Bitmap reduces image quality.

Thanks in advance!

+4
1
  • "Dispose". .
  • , , "". , , , .
  • . - , ? . vs , , . , .

Bitmap , using, :

using (Bitmap myBitmap = new Bitmap(src))
{
  //Do stuff with the temp bitmap
}
+4

Source: https://habr.com/ru/post/1545774/


All Articles