1

Why would passing a compatible DC and the DC the compatible one is based on to CreateCompatibleBitmap() give different results?

This one creates a monochrome bitmap:

CDC dcMem; 
dcMem.CreateCompatibleDC(mydc);
destBitmap->CreateCompatibleBitmap(&dcMem, rect.Width(), rect.Height());
CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);
// ... Draw on to the DC ....
dcMem.SelectObject (pBmpOld);

This one creates the correct color bitmap:

CDC dcMem; 
dcMem.CreateCompatibleDC(mydc);
destBitmap->CreateCompatibleBitmap (mydc, rect.Width(), rect.Height());
CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);
// ... Draw on to the DC ....
dcMem.SelectObject (pBmpOld);

TIA!!

df234987
  • 513
  • 2
  • 13
  • 5
    See the remarks under [CreateCompatibleBitmap](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createcompatiblebitmap). – dxiv May 13 '20 at 00:50
  • 4
    I remember the issue dxiv is pointing out biting me 20 years ago... A tip for doing win32/MFC development: the first, say, 5-10 times you use an API call, read the corresponding MSDN article from start to end. It may seem like this will take a lot of time, and it will, but it will pay off - there are many implicit assumptions and contextual knowledge for pretty much every call. But the documentation is top notch and reading it often and knowing it well is key to being able to use the APIs. – Roel May 13 '20 at 01:02

1 Answers1

1

As per the comments, have a look at the CreateCompatibleBitmap documentation:

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context, as shown in the following code ...

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164