Problems using CDC::BitBlt

Hello,

I am using BitBlt to copy one CDC to another. However, for some reason, it is not working at all. The line of code is:

pDC->BitBlt(0,0,screenWidth,screenHeight,buffer,buffer->GetWindowOrg().x,buffer->GetWindowOrg().y,SRCCOPY);

I am CERTAIN that buffer contains the graphics I need. buffer was created with the following line:

ASSERT(!buffer->CreateCompatibleDC(pDC)== 0);

Any help would be much appreciated!

-Brent



Answer this question

Problems using CDC::BitBlt

  • enric vives

    After creating the DC, you also must SelectObject a bitmap into it before you can start drawing.

    HDC memDC = CreateCompatibleDC ( hDC );
    HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
    SelectObject ( memDC, memBM );



  • Andrew Mackie

    Hi Brent

    It's hard to say what might be wrong without having more information.

    Be aware that the MFC ASSERT macros evaluate to nothing in the release build. So the call 'buffer->CreateCompatibleDC(pDC)== 0' will not get executed at all.

    Use the macro VERIFY instead for this purpose.

    Michael



  • MarcoB

    Hello,

    I have come back to this problem after a few weeks and I still am pulling my hair out.  My platform is Windows Mobile 5 Handheld.  I am attempting to perform a BitBlt from an in-memory CDC created with CreatedCompatibleDC() to a DC that points to the handheld screen.  I am entirely sure the code paints something to the in-memory CDC, as it works perfectly when I paint directly to the handheld screen DC.

    The idea is to store a "picture" of a drawing, so that I only have to BitBlt the picture instead of generating the drawing again.  Here is a fuller version of my code (pDC comes directly from an OnDraw() event handler, screenWidth = 200, screenHeight = 268):

    buffer = new CDC();

    buffer->CreateCompatibleDC(pDC);

    // debugging code, a stand-in for my real drawing code

    buffer->Ellipse(90,90,100,100);

    pDC->BitBlt(0,0,screenWidth,screenHeight,buffer,0,0,SRCCOPY);


  • Problems using CDC::BitBlt