c++imagebitmapmfccbitmap

MFC copy part of the screen into a CBitmap


Using the function

OnEraseBkgnd(CDC* pDC)

I write on the CDialog derived class, a background image that fills the screen.

Then inside the OnPaint I have the following code that is executed only once (for the 1st time OnPaint is called).

    GetInfoBarRect(&m_InfoBarRect);
    m_InfoBarBGBitmap.CreateCompatibleBitmap(&dc, m_InfoBarRect.Width(), m_InfoBarRect.Height() );

    bdc.CreateCompatibleDC(&dc);    
    pOldBitmap = bdc.SelectObject(&m_InfoBarBGBitmap);

    bdc.BitBlt (m_InfoBarRect.left, m_InfoBarRect.top, m_InfoBarRect.Width(),m_InfoBarRect.Height(), &dc, 0, 0, SRCCOPY);



    CImage image;
    image.Attach(m_InfoBarBGBitmap);
    image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatBMP);

    bdc.SelectObject(pOldBitmap);   
    bdc.DeleteDC();

The above code, copies the m_InfoBarRect part of the screen in the memory CBitmap.

Intead of having the part of the background image, I only get a blank filled rectangle with correct dimensions.

Is there something wrong with my code ?


Solution

  • You are blitting from the wrong coordinate to the wrong coordinate. Your call should be

    bdc.BitBlt( 0, 0, m_InfoBarRect.Width(), m_InfoBarRect.Height(), &dc,
                m_InfoBarRect.left, m_InfoBarRect.top, SRCCOPY);
    

    instead, i.e. blitting from the correct source position (m_InfoBarRect.left/m_InfoBarRect.top) to the destination's origion (0/0). This is assuming, that GetInfoBarRect() returns coordinates from the same coordinate system as your source DC.