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
Asked
Active
Viewed 1,186 times
1

ali
- 10,927
- 20
- 89
- 138
-
2Does this help? http://stackoverflow.com/q/4598872/3747990 – Niall Feb 18 '16 at 09:18
1 Answers
1
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")
Edit
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;
}

Barmak Shemirani
- 30,904
- 6
- 40
- 77
-
-
1You can set background brush to zero, then paint in `WM_PAINT`, see edit. If you really need background brush then you have to resize image in `WM_SIZE`, use memory dc to resize image, etc. to create a new `HBITMAP` – Barmak Shemirani Feb 18 '16 at 17:30