Copying a bitmap to clipboard | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 5, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



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();
}
Advertisement

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();
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.