Thread: BitBlt turns entire image black?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    31

    Unhappy BitBlt turns entire image black?

    Ok, I have the following code in my window processes function (where you would expect it in the switch-case block):

    Code:
    case WM_PAINT:
          {
             PAINTSTRUCT Ps;
             HDC hDC = BeginPaint(hwnd, &Ps);
    
            HBITMAP graph = CreateCompatibleBitmap(hDC, 400, 300);
             int x, y;
            SelectObject(hDC, graph);
            for( x = 0; x < 400; x++)
            for( y = 0; y < 300; y++)
            SetPixel(hDC, x, y, RGB(255, 0, 0));
    
    		EndPaint(hwnd, &Ps);
             break;
           }
    And it does exactly what I expect it to, draw a big red rectangle in the window.

    However, if I do the following:

    Code:
    case WM_PAINT:
          {
             PAINTSTRUCT Ps;
             HDC hDC = BeginPaint(hwnd, &Ps);
             HDC testbit = CreateCompatibleDC(hDC);
    
            HBITMAP graph = CreateCompatibleBitmap(testbit, 400, 300);
             int x, y;
            SelectObject(testbit, graph);
            for( x = 0; x < 400; x++)
            for( y = 0; y < 300; y++)
            SetPixel(testbit, x, y, RGB(255, 0, 0));
    
             BitBlt(hDC,0,0,400,300,testbit,0,0,SRCCOPY);
             
          DeleteDC(testbit);
    		EndPaint(hwnd, &Ps);
             break;
          }
    The rectangle I get is black. Can anybody tell me why this is?

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Ok, so I changed a line in the non-functioning section to
    Code:
    HBITMAP graph = CreateCompatibleBitmap(hDC, 400, 300);
    and it seems to work fine. I guess it makes sense that I should be making it compatible with the window's device context itself, but I would think that compatibility would be transitive in that if Bitmap A is transitive with Device Context B, and Device Context B is compatible with Device Context C, A would be just as compatible with C as it is with B. Could anyone explain exactly why it is that way?

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    My guess is that while Device Context B is compatible, it's still a memory device context, but Device Context A is a screen device context.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>HBITMAP graph = CreateCompatibleBitmap(testbit, 400, 300);

    When created a memory DC contains a 1x1 monochrome bitmap regardless of the colour depth of the DC it was created compatible with/from.

    Therefore the line above (in your code) creates a monochrome bitmap compatible with the default bitmap already in the testbit DC.



    Look up GDI leaks as you have a few bad ones. You must put the default GDI objects back before the DC will delete. Bitmaps may not delete while selected into a DC. A .NET version IDE will fix most of these leaks.

    Try moving another window over your app. It will soon fail to redraw. Can watch the GDI object count in the Task Manager (need to add GDI to the colums displayed).



    PS
    A quicker/better way would be to create the big red rect DC on a WM_CREATE msg, use it in the WM_PAINT msg and clean it up in WM_CLOSE. Make it a static, if local to the callback or global to the window.
    Last edited by novacain; 05-01-2006 at 03:37 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  3. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. structure with 2-dim array
    By sballew in forum C Programming
    Replies: 8
    Last Post: 10-27-2001, 04:30 PM