Copy a bitmap from another HBITMAP

I am trying to write a class to wrap the functionality of a bitmap in my program.

One useful feature is to copy a bitmap from another bitmap descriptor. I'm a little 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 for the original bitmap if everything I have in the original bitmap is a descriptor (e.g. HBITMAP?)

You can see in the code above, I used "dcSource" in the BitBlt () call. But I don't know how to get this dcSource from the source raster descriptor (bmp.Handle () returns the descriptor of the original raster image)

+4
c ++ windows bitmap atl gdi
source share
2 answers

You cannot - the original bitmap cannot be selected in DC at all, and even if you don’t have a way to find out which DC.

To make a copy, you probably want to use something like:

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

Then you can switch from source to target DC.

+7
source share

Worked for me:

 // hBmp is a HBITMAP HBITMAP hBmpCopy= (HBITMAP) CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE); 
+1
source share

All Articles