Currently viewing: GipsySoft » Front Page» Articles

CopyBitmapToClipboard

Given a HBITMAP copy it to the clipboard

bool CopyBitmapToClipboard( HWND hwnd, HBITMAP hbm )

Return value

Returns true on success.

Example

This example copies the top left quarter of the screen to the clipboard.

//
//	Get some widths, only copy the top left quarter of the screen
const int nWidth = GetSystemMetrics( SM_CXSCREEN ) / 2;
const int nHeight = GetSystemMetrics( SM_CYSCREEN ) / 2;

//
//	Get a device context to the screen
HDC hdc = GetDC( NULL );

//
//	Get a memory device context (compatible)
HDC hdcMem = CreateCompatibleDC( hdc );

//
//	Create a bitmap (compatible with the screen and not our memory device!)
HBITMAP hbm = CreateCompatibleBitmap( hdc, nWidth, nHeight );

//
//	Select our bitmap into our device context
HGDIOBJ hOld = SelectObject( hdcMem, hbm );

//
//	Copy from the screen to our bitmap
BitBlt( hdcMem, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY );

//
//	A bit of cleanup
SelectObject( hdcMem, hOld );
DeleteDC( hdcMem );
ReleaseDC( NULL, hdc );

//
//	Copy our bitmap to the clipboard, now go and 'paste' into MS Paint and you
//	will have a copy of the screen (well, the top left quarter of it anyway).
//	Notice that we do not delete the bitmap (hbm) because it is now owned by
//	the clipboard.
CopyBitmapToClipboard( NULL, hbm );

 

Click to view the source code

See also none