PRB: Memory DC Produces Monochrome Images

This article was previously published under Q139165
This article has been archived. It is offered "as is" and will no longer be updated.
SYMPTOMS
When you create and use a memory Device Context (DC) to draw and store GDIimages, the bitmap displayed when transferred from the memory devicecontext to the physical display is monochrome. All colors are converted toeither black or white.
CAUSE
When creating a compatible bitmap for the memory DC, the handle to thememory DC is used as the first parameter to the call toCreateCompatibleBitmap(). This create a monochrome bitmap because amemory DC created with CreateCompatibleDC() is given a 1x1 monochrome bitmap as its default bitmap.

For example, the following code creates a monochrome bitmap and selects itinto a memory DC:
   HDC hdc, hdcMem;   HBITMAP hBitmap;   hdc = GetDC(hWnd);   hdcMem = CreateCompatibleDC(hdc);   hBitmap = CreateCompatibleBitmap(hdcMem, 400, 400);   SelectObject(hdcMem, hBitmap);   SetTextColor(hdcMem, RGB(0, 0, 255));				
RESOLUTION
Send a physical screen DC to the CreateCompatibleBitmap() function rather than the memory DC that you plan on selecting the bitmap into.

The following example code properly sets up a color memory DC with a colorbitmap with the same bit depth as the physical display:
   HDC hdc, hdcMem;   HBITMAP hBitmap;   hdc = GetDC(hWnd);   hdcMem = CreateCompatibleDC(hdc);   hBitmap = CreateCompatibleBitmap(hdc, 400, 400);   SelectObject(hdcMem, hBitmap);   SetTextColor(hdcMem, RGB(0, 0, 255));				
STATUS
This behavior is by design.
3.10 4.00
Properties

Article ID: 139165 - Last Review: 02/24/2014 08:23:29 - Revision: 2.1

  • Microsoft Windows Software Development Kit 3.1
  • Microsoft Win32 Application Programming Interface
  • kbnosurvey kbarchive kbprb kbcode KB139165
Feedback