简体   繁体   English

MFC将部分屏幕复制到CBitmap中

[英]MFC copy part of the screen into a CBitmap

Using the function 使用该功能

OnEraseBkgnd(CDC* pDC) OnEraseBkgnd(CDC * pDC)

I write on the CDialog derived class, a background image that fills the screen . 我在CDialog派生类上写了一个填充屏幕的背景图像。

Then inside the OnPaint I have the following code that is executed only once (for the 1st time OnPaint is called). 然后在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();

The above code, copies the m_InfoBarRect part of the screen in the memory CBitmap. 上面的代码,复制了内存CBitmap中屏幕的m_InfoBarRect部分。

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 ? 我的代码有问题吗?

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, ie blitting from the correct source position ( m_InfoBarRect.left / m_InfoBarRect.top ) to the destination's origion ( 0 / 0 ). 代替,即,从正确的源位置(位图传输m_InfoBarRect.left / m_InfoBarRect.top )到目的地的origion( 0 / 0 )。 This is assuming, that GetInfoBarRect() returns coordinates from the same coordinate system as your source DC. 这是假设, GetInfoBarRect()从与源DC相同的坐标系返回坐标。

I think you might want: 我想你可能想要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM