Copying a bitmap from another HBITMAP

隐身守侯 提交于 2019-12-10 14:21:58

问题


I'm trying to write a class to wrap bitmap functionality in my program.

One useful feature would be to copy a bitmap from another bitmap handle. I'm a bit stuck:

    void operator=( MyBitmapType & bmp )
    {
        HDC dcMem;
        HDC dcSource;

        if( m_hBitmap != bmp.Handle() )
        {
            if( m_hBitmap )             
                this->DisposeOf();

            // copy the bitmap header from the source bitmap
            GetObject( bmp.Handle(), sizeof(BITMAP), (LPVOID)&m_bmpHeader );

            // Create a compatible bitmap
            dcMem       = CreateCompatibleDC( NULL );
            m_hBitmap   = CreateCompatibleBitmap( dcMem, m_bmpHeader.bmWidth, m_bmpHeader.bmHeight );

            // copy bitmap data
            BitBlt( dcMem, 0, 0, bmp.Header().bmWidth, bmp.Header().bmHeight, dcSource, 0, 0, SRCCOPY );
        }
    }

This code is missing one thing: How can I get an HDC to the source bitmap if all I have of the source bitmap is a handle (e.g. an HBITMAP?)

You can see in the code above, I've used "dcSource" in the BitBlt() call. But I don't know how to get this dcSource from the source bitmap's handle (bmp.Handle() returns the source bitmaps handle)


回答1:


You can't -- the source bitmap may not be selected into a DC at all, and even if it is you have no way to find out what DC.

To do your copy, you probably want to use something like:

dcSrc = CreateCompatibleDC(NULL);
SelectObject(dcSrc, bmp);

Then you can blit from the source to destination DC.




回答2:


Worked for me:

// hBmp is a HBITMAP 
HBITMAP hBmpCopy= (HBITMAP) CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE);


来源:https://stackoverflow.com/questions/5687263/copying-a-bitmap-from-another-hbitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!