Virtual Developer Workshop: Containerized Development with Docker

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 )
	::EmptyClipboard() ;
	if( pPal )
		::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
	::SetClipboardData (CF_BITMAP, bitmap.GetSafeHandle() ) ;
	::CloseClipboard () ;
	if( pPal )

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 )
	if( pPal )
		::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
	::SetClipboardData (CF_DIB, hDIB ) ;
	::CloseClipboard () ;
	if( pPal )

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;



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



  • how to display a BMP file from clipboard

    Posted by Legacy on 02/17/2004 08:00am

    Originally posted by: liping

    One image is put on the clipboard. Now How do I take the data from the clipboard and display it on the window?
    Thank you for help.

  • Image->Matrix

    Posted by Legacy on 01/17/2004 08:00am

    Originally posted by: Bassem

    hello all
    i wanna convert a picture (bmp) to a matrix and the vice
    can any one help me please, it's urgent

  • Caution in copying DIB data into clipboard

    Posted by Legacy on 09/17/2003 07:00am

    Originally posted by: Jaeyoun Yi

    When copying DIB data into clipboard,
    you must pay attention to the allocation and deallocation of the data handle, hDIB.

    The data handle hDIB must be allocated using GlobalAlloc with GMEM_MOVEABLE flag, not with GMEM_FIXED.
    And you must NOT free the data handle even after your application calls ::CloseClipboard(),
    although the MSDN says we can do.

    Instead of freeing the data handle right after calling ::CloseClipboard(),
    you can GlobalFree hDIB in WM_DESTROYCLIPBOARD message handler of the clipboard owner.

    Otherwise, the other application may refer to invalid data handle or your application may fail in every second clipboard-copy procedure.

    Jaeyoun Yi. ROK

  • Function 3: Caution!

    Posted by Legacy on 07/22/2003 07:00am

    Originally posted by: Oliver Twesten

    Function 3 (CopyWndToClipboard()) will only work correctly when the whole window is inside the desktop rectangle. If you move the window e.g. halfway out of the visible screen, the copied image will be corrupted.

    To prevent this, you'd have to add some checks and deflate the copy rect according to the screen rect.


  • CBitmap::GetBitmap() function

    Posted by Legacy on 04/15/2003 07:00am

    Originally posted by: Richard J. Lee

    I need color value for each pixel.
    Can do I do this with GetBitmap() function and
    using BITMAP.bmBits fields ?

    Thanks in advance.

  • Doubts...

    Posted by Legacy on 02/16/2003 08:00am

    Originally posted by: Wil

    I tried to use the 3rd function, including it into my code, exactly as it is written. But when I try to compile it, I get the following erros (4):

    Snapshot.c(103) : error C2143: syntax error : missing ')' before '*'

    Snapshot.c(103) : error C2143: syntax error : missing '{' before '*'

    Snapshot.c(103) : error C2059: syntax error : ')'

    Snapshot.c(104) : error C2054: expected '(' to follow 'pWnd'

    Does somebody know why these occours ?

    PS: I'm starting with visual C++ 6 and I'd really appreciate any help...



  • Use CWindowDC instead of CClientDC

    Posted by Legacy on 02/05/2003 08:00am

    Originally posted by: njkayaker

    ... in example 3

  • Clipboard Problem On win2000 & XP

    Posted by Legacy on 06/20/2002 07:00am

    Originally posted by: Rehan

    hi i am using following code to send bitmap into clipboard 
    it work on winme and 98 but fail on 2000 and xp after following error message when trying to paste in MSPAINT

    "Error getting clipboard data"

    i have handle to bitmap and want to send it on clipboard

    if( AfxGetApp()->m_pMainWnd->OpenClipboard() )
    SetClipboardData(CF_BITMAP, m_hBitmap);


    Any one got an idea

  • Function 2: .bitmap not required

    Posted by Legacy on 01/18/2001 08:00am

    Originally posted by: Bryan Anslow

    In the function 2:, there is a bitmap.Detach(), but there is no definition of bitmap.
    I am guessing that this line was inadvertantly left in and is not required?


  • How to copy the not visible dc???

    Posted by Legacy on 01/09/2001 08:00am

    Originally posted by: Ricky Berghold

    Question: How i can copy a DC which is greater than the visible DC. ( i have scrollbars and i want to copy the whole DC!!!)

    Can help someone???

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date