dcsimg
 

Copying a bitmap to clipboard

Wednesday Aug 5th 1998 by Zafir Anjum

Copying a bitmap to clipboard


Copying a bitmap to the clipboard is fairly simple. The one thing to remember is that you should also copy the palette if the bitmap needs a palette to display properly.

Function 1: Copy a device-dependent bitmap to clipboard

The CopyBitmapToClipboard() function shown below copies the DDB to the clipboard. It also copies the palette to the clipboard if one is supplied. Note the calls to Detach() at the end. This is important since the ownership of the GDI object has been transferred to the clipboard.
// CopyBitmapToClipboard	- Copies a device-dependent bitmap to clipboard
// pWnd				- Pointer to window that opens the clipboard
// bitmap			- The device-dependent bitmap
// pPal				- Pointer to logical palette - Can be NULL
// NOTE				- GDI objects are detached from bitmap & pPal
//				  as the clipboard owns them after the copy
void CopyBitmapToClipboard( const CWnd *pWnd, CBitmap& bitmap, CPalette* pPal )
{
	::OpenClipboard(pWnd->GetSafeHwnd());
	::EmptyClipboard() ;
	if( pPal )
		::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
	::SetClipboardData (CF_BITMAP, bitmap.GetSafeHandle() ) ;
	::CloseClipboard () ;
	bitmap.Detach();
	if( pPal )
		pPal->Detach();
}

Function 2: Copy a device-independent bitmap to clipboard

The CopyDIBToClipboard() function is very similar to the CopyBitmapToClipboard() function. The memory handle containing the BITMAPINFO and the bitmap bits should have been allocated using GlobalAlloc(). Once the DIB has been copied to the clipboard, the memory handle is owned by the clipboard and should not be released by your application.
// CopyDIBToClipboard	- Copies a device-dependent bitmap to clipboard
// pWnd			- Pointer to window that opens the clipboard
// hDIB			- Memory handle that contains BITMAPINFO & bitmap bits
// pPal			- Pointer to logical palette - Can be NULL
// NOTE			- GDI objects are detached from bitmap & pPal
//			  as the clipboard owns them after the copy
void CopyDIBToClipboard( const CWnd *pWnd, HGLOBAL hDIB, CPalette* pPal )
{
	::OpenClipboard(pWnd->GetSafeHwnd());
	::EmptyClipboard();
	if( pPal )
		::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
	::SetClipboardData (CF_DIB, hDIB ) ;
	::CloseClipboard () ;
	bitmap.Detach();
	if( pPal )
		pPal->Detach();
}

Function 3: Copy a window image to clipboard

The CopyWndToClipboard() function copies the image of the window to the clipboard.
void CopyWndToClipboard( CWnd *pWnd )
{
	CBitmap 	bitmap;
	CClientDC	dc(pWnd);
	CDC 		memDC;
	CRect		rect;

	memDC.CreateCompatibleDC(&dc); 

	pWnd->GetWindowRect(rect);

	bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
	
	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
	memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY); 

	pWnd->OpenClipboard() ;
	EmptyClipboard() ;
	SetClipboardData (CF_BITMAP, bitmap.GetSafeHandle() ) ;
	CloseClipboard () ;

	memDC.SelectObject(pOldBitmap);
	bitmap.Detach();
}

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved