从另一个 HBITMAP 复制位图
我正在尝试编写一个类来在我的程序中包装位图功能。
一个有用的功能是从另一个位图句柄复制位图。我有点卡住了:
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 获取到源位图?
您可以在上面的代码中看到,我在 BitBlt() 调用中使用了“dcSource”。但我不知道如何从源位图的句柄中获取此 dcSource (bmp.Handle() 返回源位图句柄)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能——源位图可能根本不会被选入 DC,即使是,你也无法找出是什么 DC。
要进行复制,您可能需要使用类似以下内容:
然后您可以从源 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:
Then you can blit from the source to destination DC.
为我工作:
Worked for me: