从另一个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 );
}
}
这code缺少一件事:我怎样才能得到一个HDC的源位图,如果我所有的源位图的是一个句柄(?例如一个HBITMAP)
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?)
您可以在code以上,我用dcSource中的BitBlt()调用见。但我不知道如何从源位图的句柄得到这个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块复制。
Then you can blit from the source to destination DC.
这篇关于从另一个HBITMAP复制一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!