Reputation: 11045
I have used the code here to load a PNG image into a BMP raw vector std::vector <unsigned char>
. Now, I need to apply this image as background to a WinAPI window and I don't know how could I convert it to HBITMAP
. Maybe somebody did it before or maybe I could use another format or variable type
Upvotes: 1
Views: 1374
Reputation: 31629
You can use Gdiplus from the start, to open png file and get HBITMAP
handle
//initialize Gdiplus:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HBITMAP hbitmap;
HBRUSH hbrush;
Gdiplus::Bitmap *bmp = Gdiplus::Bitmap::FromFile(L"filename.png");
bmp->GetHBITMAP(0, &hbitmap);
hbrush = CreatePatternBrush(hbitmap);
//register classname and assign background brush
WNDCLASSEX wcex;
...
wcex.hbrBackground = hbrush;
CreateWindow...
Cleanup:
DeleteObject(hbrush);
DeleteObject(hbitmap);
delete bmp;
Gdiplus::GdiplusShutdown(gdiplusToken);
You need to include "gdiplus.h" and link to "gdiplus.lib" library. The header files should be available by default.
In Visual Studio you can link to Gdiplus as follows:
#pragma comment( lib, "Gdiplus.lib")
or use Gdiplus::Image
in WM_PAINT
Gdiplus::Image *image = Gdiplus::Image::FromFile(L"filename.png");
WM_PAINT
in Window procedure:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (image)
{
RECT rc;
GetClientRect(hwnd, &rc);
Gdiplus::Graphics g(hdc);
g.DrawImage(image, Gdiplus::Rect(0, 0, rc.right, rc.bottom));
}
EndPaint(hwnd, &ps);
return 0;
}
Upvotes: 1