Copying a bitmap to clipboard
Posted
by Zafir Anjum
on August 5th, 1998
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:00amOriginally posted by: liping
ReplyImage->Matrix
Posted by Legacy on 01/17/2004 12:00amOriginally posted by: Bassem
hello all
Replyi wanna convert a picture (bmp) to a matrix and the vice
can any one help me please, it's urgent
Thanks
Caution in copying DIB data into clipboard
Posted by Legacy on 09/17/2003 12:00amOriginally 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,
ReplyJaeyoun Yi. ROK
Function 3: Caution!
Posted by Legacy on 07/22/2003 12:00amOriginally 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.
ReplyCBitmap::GetBitmap() function
Posted by Legacy on 04/15/2003 12:00amOriginally 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.
ReplyDoubts...
Posted by Legacy on 02/16/2003 12:00amOriginally 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
ReplyUse CWindowDC instead of CClientDC
Posted by Legacy on 02/05/2003 12:00amOriginally posted by: njkayaker
... in example 3
Reply
Clipboard Problem On win2000 & XP
Posted by Legacy on 06/20/2002 12:00amOriginally posted by: Rehan
ReplyFunction 2: .bitmap not required
Posted by Legacy on 01/18/2001 12:00amOriginally 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.
ReplyHow to copy the not visible dc???
Posted by Legacy on 01/09/2001 12:00amOriginally 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???
ReplyLoading, Please Wait ...