hackjutsu hackjutsu - 1 month ago 13
C++ Question

Failure for Gdiplus::Bitmap::FromHICON

I'm trying to convert a

HICON
to
Gdiplus::Bitmap
by calling
Gdiplus::Bitmap::FromHICON
as shown below.

I created a windows icon by calling
LoadIcon
, and verified the icon is valid by drawing on screen.

Surprisingly, I still got a
NULL
pointer. I revisited the win32 documents but cannot find what I'm missing.

HICON hIcon = LoadIcon(NULL, IDI_WINLOGO);
assert( hIcon != nullptr ); // passed

Gdiplus::Bitmap *pIcon = Gdiplus::Bitmap::FromHICON(hIcon);

assert( pIcon != nullptr ); // failed


Does anyone have some idea? Thanks:)

Ron Ron
Answer Source

You need to initialize the GDI+ engine using the GdiplusStartup function first:

HICON hIcon = LoadIcon(NULL, IDI_WINLOGO);
assert(hIcon != nullptr); // passed
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Gdiplus::Bitmap *pIcon = Gdiplus::Bitmap::FromHICON(hIcon);
assert(pIcon != nullptr); // now OK
GdiplusShutdown(gdiplusToken);