从另一个 HBITMAP 复制位图 [英] Copying a bitmap from another HBITMAP
问题描述
我正在尝试编写一个类来在我的程序中包装位图功能.
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 );
}
}
此代码缺少一件事:如果我拥有的所有源位图只是一个句柄(例如 HBITMAP?),我怎样才能获得源位图的 HDC?
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?)
您可以在上面的代码中看到,我在 BitBlt() 调用中使用了dcSource".但是不知道怎么从源位图的句柄中得到这个dcSource(bmp.Handle()返回源位图句柄)
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)
推荐答案
你不能——源位图可能根本没有被选入 DC,即使是你也没有办法找出是什么 DC.
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);
然后您可以从源 DC 到目标 DC.
Then you can blit from the source to destination DC.
这篇关于从另一个 HBITMAP 复制位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!