hackjutsu
hackjutsu

Reputation: 8922

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

Upvotes: 0

Views: 316

Answers (1)

Ron
Ron

Reputation: 15501

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

Upvotes: 2

Related Questions