如何释放从 GdipCreateBitmapFromHBITMAP 创建的对象?

How to release an object created from GdipCreateBitmapFromHBITMAP?

我正在编写一个 C 应用程序,在我的代码中我正在调用构造函数 GdipCreateBitmapFromHBITMAP

我知道不应从 C 调用构造函数,但我使用了此处的 "hack"“How can I take a screenshot and save it as JPEG on Windows?

我不确定如何释放在调用 GdipCreateBitmapFromHBITMAP 期间分配的资源。

我尝试在文档中寻求帮助,但一无所获。

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

如何释放pBitmap

非常感谢。

您正在做的不是 "hack"。虽然 GDI+ 主要设计用于 C++,但它确实公开了一个 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) 构造函数。

也就是说,GdipCreateBitmapFromHBITMAP() returns 指向 GpBitmap 对象的指针(在 C++ 中被 Bitmap class in C++). GdipDisposeImage() is the correct way to release that object (which is called by the Image 析构函数包装)。

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);