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



Comments

  • how to display a BMP file from clipboard

    Posted by Legacy on 02/17/2004 12: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.
    liping

    Reply
  • Image->Matrix

    Posted by Legacy on 01/17/2004 12: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
    Thanks

    Reply
  • Caution in copying DIB data into clipboard

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

    Originally posted by: Jaeyoun Yi

    Hi,
    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.

    Regards,
    Jaeyoun Yi. ROK

    Reply
  • Function 3: Caution!

    Posted by Legacy on 07/22/2003 12: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.

    Oliver.

    Reply
  • CBitmap::GetBitmap() function

    Posted by Legacy on 04/15/2003 12: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.

    Reply
  • Doubts...

    Posted by Legacy on 02/16/2003 12: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...

    Thanks.

    Wil

    Reply
  • Use CWindowDC instead of CClientDC

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

    Originally posted by: njkayaker

    ... in example 3

    Reply
  • Clipboard Problem On win2000 & XP

    Posted by Legacy on 06/20/2002 12: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() )
    {
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, m_hBitmap);
    CloseClipboard();

    }

    Any one got an idea

    Reply
  • Function 2: .bitmap not required

    Posted by Legacy on 01/18/2001 12: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?

    Bryan.

    Reply
  • How to copy the not visible dc???

    Posted by Legacy on 01/09/2001 12: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???

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds