Copying bitmap into clipboard
Hi,
I am trying to use following code for copying bitmap into clipboard,
but when I try to paste into mspaint, then result is only black square.
What is wrong with following code ?
void CopyBitmapIntoClipboard(HBITMAP bitmaph)
{
CBitmap *bitmapFromGraphList = CBitmap::FromHandle(bitmaph);
BITMAP bitmapStruct;
memset(&bitmapStruct, 0, sizeof(bitmapStruct));
bitmapFromGraphList->GetObject(sizeof(BITMAP),&bitmapStruct);
CBitmap localBitmap;
localBitmap.CreateBitmapIndirect(&bitmapStruct);
CDC dcMemSrc, dcMemDest;
dcMemSrc.CreateCompatibleDC(NULL);
CBitmap* oldBitmapSrc = dcMemSrc.SelectObject(bitmapFromGraphList);
dcMemDest.CreateCompatibleDC(NULL);
CBitmap* oldBitmapDest=dcMemDest.SelectObject(&localBitmap);// It returns
NULL !
dcMemDest.BitBlt(0,0,bitmapStruct.bmWidth, bitmapStruct.bmHeight, &dcMemSrc,
0, 0, SRCCOPY);
HBITMAP toClipboard = (HBITMAP)localBitmap.Detach();
dcMemDest.SelectObject(oldBitmapDest);
dcMemSrc.SelectObject(oldBitmapSrc);
if ( !OpenClipboard() )
return;
if( !EmptyClipboard() )
return;
HANDLE setDataHandle = ::SetClipboardData( CF_BITMAP, toClipboard );
if (setDataHandle == NULL ) {
CloseClipboard();
return;
}
CloseClipboard();
}