windows 从另一个 HBITMAP 复制位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5687263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying a bitmap from another HBITMAP
提问by jtsPimp
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?)
这段代码缺少一件事:如果我拥有的所有源位图是一个句柄(例如 HBITMAP?),我怎样才能获得源位图的 HDC?
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)
您可以在上面的代码中看到,我在 BitBlt() 调用中使用了“dcSource”。但是我不知道如何从源位图的句柄中获取这个 dcSource(bmp.Handle() 返回源位图句柄)
回答by Jerry Coffin
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.
你不能——源位图可能根本没有被选入 DC,即使是你也无法找出什么 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.
然后你可以从源到目标 DC 进行 blit。
回答by sergiol
Worked for me:
对我来说有效:
// hBmp is a HBITMAP
HBITMAP hBmpCopy= (HBITMAP) CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE);