1

I'm writing a C application and in my code I'm calling the constructor GdipCreateBitmapFromHBITMAP.

I know the constructor should not be called from C, but I used a "hack" from here "How can I take a screenshot and save it as JPEG on Windows?"

I'm not sure how to release resources that were allocated during the call to GdipCreateBitmapFromHBITMAP.

I tried finding help in the documentation but found nothing.

ULONG *pBitmap = NULL;
lGdipCreateBitmapFromHBITMAP = (pGdipCreateBitmapFromHBITMAP)GetProcAddress(hModuleThread, "GdipCreateBitmapFromHBITMAP");
lGdipCreateBitmapFromHBITMAP(hBmp, NULL, &pBitmap);

How to I release pBitmap?

Thanks a lot.

Community
  • 1
  • 1
Michael
  • 796
  • 11
  • 27
  • C does not have constructors. no problem code is posted, suggest closing this question – user3629249 May 02 '15 at 16:30
  • Of course C does not have constructors, but a constructor is simply a normal function. And by using some "hacks" (as described above) I can call the Bitmap constructor from C and create a valid Bitmap object. – Michael May 02 '15 at 16:57
  • 1
    are you looking for `GdipDisposeImage()` – Alex K. May 02 '15 at 17:22
  • Sounds so ! Will a call to GdipDisposeImage be enough in order to completely release all the resources allocated at GdipCreateBitmapFromHBITMAP ? P.S Please post an answer so I can mark your response as correct. – Michael May 02 '15 at 17:41
  • 1
    @RemyLebeau has you covered. `hBmp` will need `DeleteObject()` as well – Alex K. May 02 '15 at 19:53

1 Answers1

5

What you are doing is not a "hack". Although GDI+ is primarily designed to be used in C++, it does expose a flat API for use in C. Your code is using that API. GdipCreateBitmapFromHBITMAP() is not a constructor, it is a flat C function that is called by the Bitmap.Bitmap(HBITMAP, HPALETTE) constructor.

That being said, GdipCreateBitmapFromHBITMAP() returns a pointer to a GpBitmap object (which is wrapped by the Bitmap class in C++). GdipDisposeImage() is the correct way to release that object (which is called by the Image destructor in C++).

struct GpImage {};
struct GpBitmap {};
typedef GpStatus (WINGDIPAPI *pGdipCreateBitmapFromHBITMAP)(HBITMAP, HPALETTE hpal, GpBitmap**);
typedef GpStatus (WINGDIPAPI *GdipDisposeImage)(GpImage *image);

GpBitmap *pBitmap = NULL;
lGdipCreateBitmapFromHBITMAP = (pGdipCreateBitmapFromHBITMAP) GetProcAddress(hModuleThread, "GdipCreateBitmapFromHBITMAP");
lGdipDisposeImage = (pGdipDisposeImage) GetProcAddress(hModuleThread, "GdipDisposeImage");
//...
lGdipCreateBitmapFromHBITMAP(hBmp, NULL, &pBitmap);
//...
lGdipDisposeImage((GpImage*)pBitmap);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks Remy for your contribution. Any idea why `GdipCreateBitmapFromHBITMAP` takes so much memory? for bitmap with a resolution of 2560 x 1440, it takes around 14448 MB of RAM. – RCECoder Jun 06 '19 at 06:26