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 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,523 questions
C++
C++
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,636 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,206 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,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.