PRB: CreateCompatibleBitmap() Behaves Differently on NT and 95

Last reviewed: December 9, 1996
Article ID: Q160522
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) for Windows NT & Win95, versions 3.51, 4.00

SYMPTOMS

When calling CreateCompatibleBitmap() on Windows NT, the operating system will return a bitmap where all of the pixels are initialized to BLACKNESS. On Windows 95, however, the operating system will not initialize the bits. This behavioral difference frequently shows up when software is developed on Windows NT and then later tested on Windows 95.

CAUSE

Windows NT initializes the contents of the bitmap for security reasons. Windows 95 does not provide the same level of security as Windows NT and, therefore, does not initialize the bits. You should always treat newly-created memory bitmaps as though they were uninitialized. You should initialize them yourself prior to drawing on them.

RESOLUTION

Fortunately, this behavioral difference is easy to work around. The simplest way is to PatBlt() the surface of the memory bitmap with BLACKNESS after it is created. For example, to alter the function below so it produces the same behavior on both Windows 95 and Windows NT, add the following line of code immediately after the call to SelectObject():

   PatBlt(hdc, 0,0, rect.right, rect.bottom, BLACKNESS);

STATUS

This behavior is by design.

REFERENCES

The following code fragment demonstrates the problem. When you run it on Windows NT, it will set the client area of the window corresponding to the HWND parameter to all black. When you run the same code on Windows 95, the window will be filled with whatever the image representing the uninitialized contents of the bitmap memory is:

   void Test(HWND hWnd)
   {
       HDC hdcMem, hdc = GetDC(hWnd);
       HBITMAP hbm;
       RECT rect;

       // Get the extents of our drawing surface
       GetClientRect(hWnd, &rect);

       // Create a memory drawing surface
       hbm = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
       hdcMem = CreateCompatibleDC(hdc);
       SelectObject(hdcMem, hbm);

   //  Uncomment next line to fix the problem
   //  PatBlt(hdc, 0,0, rect.right, rect.bottom, BLACKNESS);

       // Draw from our memory surface to our device surface
       BitBlt(hdc, 0, 0, rect.right, rect.bottom,
              hdcMem, 0, 0, SRCCOPY);

       // Cleanup
       DeleteDC(hdcMem);
       DeleteObject(hbm);
       ReleaseDC(hWnd, hdc);
   }


KBCategory: kbgraphic kbprb
KBSubcategory: GdiBmp
Additional reference words: 3.51 4.00 beta99 kbdsi



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 9, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.