Win32: BITMAP to ID2D1Bitmap

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); = GetDeviceCaps(hScreenDC, VERTRES);  
   HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, state.sw,;  
   HBITMAP hOldBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC, hBitmap));  
   BitBlt(hMemoryDC, 0, 0, state.sw,, hScreenDC, 0, 0, SRCCOPY);  
   hBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC, hOldBitmap));  
   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 API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,440 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,554 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,941 Reputation points
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,506 Reputation points Microsoft Vendor

    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.