GetDIBits + ID2D1RenderTarget::CreateBitmap from lpvBits
Win32: BITMAP to ID2D1Bitmap
![](https://techprofile.blob.core.windows.net/images/CFIB-zqfp0esdKGTs5ax_A.png?8DA8BE)
thebluetropics
1,046
Reputation points
How can I get ID2D1Bitmap
from a BITMAP
in Win32/Direct2D?
HDC hScreenDC = GetDC(nullptr);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
state.sw = GetDeviceCaps(hScreenDC, HORZRES);
state.sh = GetDeviceCaps(hScreenDC, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, state.sw, state.sh);
HBITMAP hOldBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, state.sw, state.sh, hScreenDC, 0, 0, SRCCOPY);
hBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC, hOldBitmap));
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
BITMAP bmp;
GetObject(hBitmap, sizeof(BITMAP), reinterpret_cast<void*>(&bmp));
ID2D1Bitmap* d2dBitmap = bmp; // Somehow do this
I have trouble finding the tutorial for this. So I ended up here.
Regards, @thebluetropics
Accepted answer
1 additional answer
Sort by: Most helpful
-
Xiaopo Yang - MSFT 12,231 Reputation points Microsoft Vendor
2022-09-30T01:45:13.41+00:00 Hello @thebluetropics ,
You can load a Bitmap based on decoder's implementation. For an example, see How to Load a Bitmap from a File , How to Load a Bitmap from a Resource (Direct2D).
In order to load a Bitmap, you can try GUID_ContainerFormatBmp.