Win32: BITMAP to ID2D1Bitmap

thebluetropics 1,046 Reputation points
2022-09-29T08:07:31.8+00:00

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

Windows development Windows API - Win32
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2022-09-30T06:54:12.03+00:00
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.