MFC将部分屏幕复制到CBitmap中

时间:2016-06-03 18:35:38

标签: c++ image bitmap mfc cbitmap

使用功能

  

OnEraseBkgnd(CDC * pDC)

我在CDialog派生类上写了一个填满屏幕的背景图片

然后在OnPaint中我有以下代码只执行一次(第一次调用OnPaint)。

    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();

上面的代码,复制了内存CBitmap中屏幕的m_InfoBarRect部分。

拥有背景图像的一部分,我只得到一个空白的填充矩形,尺寸正确。

我的代码有问题吗?

2 个答案:

答案 0 :(得分:3)

您从错误的坐标到错误的坐标进行blitting。你的电话应该是

bdc.BitBlt( 0, 0, m_InfoBarRect.Width(), m_InfoBarRect.Height(), &dc,
            m_InfoBarRect.left, m_InfoBarRect.top, SRCCOPY);
相反,即从正确的源位置(m_InfoBarRect.left / m_InfoBarRect.top)到目的地的起源(0 / 0)。这是假设,GetInfoBarRect()返回与源DC相同坐标系的坐标。

答案 1 :(得分:0)

我想你可能想要:

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

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