Fix Image.FromHbitmap(hBitmap) Memory Leak

Don’t forget to clean up after using Image img = Image.FromHbitmap(hBitmap) or you will experience serious memory leaks.

Image img = null;
try {
  img = Image.FromHbitmap(hBitmap);
}finally{
  DeleteObject(hBitmap);
  img.Dispose();
}
 
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
This entry was posted in c#. Bookmark the permalink.