Click to See Complete Forum and Search --> : Bitmap to Clipboard


bes
April 25th, 2005, 03:48 PM
Hi Experts,

I have the following problem: I need to copy a Bitmap to the Clipboard.
This already works fine when loading a BMP from file:

HBITMAP hBmp = LoadImage (....LR_LOADFROMFILE...);

SetClipboardData (CF_BITMAP, hBmp);

But my app creates a BMP in memory, that means I don't want to store it to file before copying it to the Clipboard. I'm using therefore:

HBITMAP hBmp = CreateBitmap (width, height, x, x, BMP_buffer);

SetClipboardData (CF_BITMAP, hBmp);

I'm getting a Bitmap with correct dimensions into the Clipboard, but the Bitmap content is either black or distored (some picture lines looks fine from time to time only), although the BMP_buffer containes correct BMP info (it looks fine when I store it to file)...

The BMP_buffer was allocated by using GlobalAlloc ()


It would be great if someone could point me to the error...or maybe tell me another way of putting the Bitmap to the Clipboard without using CBitmap.


Thanks!

hankdane
April 26th, 2005, 01:18 PM
Does BMP_Buffer include BITMAPINFO/BITMAPINFOHEADER?

bes
April 27th, 2005, 09:50 AM
No, the BMP_Buffer is only a Pointer to the RGB Data.
The BMP Header Data is passed to CreateBitmap ().

The BMP is created in the Clipboard correctly - "only" the content is wrong.
For me it seems to be a problem of BMP_Buffer -> maybe the clipboard cannot access the Data !??

Roger Allen
April 27th, 2005, 11:35 AM
Here is some code (slightly cut down) which should export a bitmap correctly:


CDC* pDC = NULL;
CDC compatDC;
CBitmap compatBitmap;

pDC = GetDC();

// draw to a compatible device context and then copy to clipboard
compatDC.CreateCompatibleDC(pDC);
compatDC.SaveDC();
compatBitmap.CreateCompatibleBitmap(pDC, bitmapRect.Width(), bitmapRect.Height());
compatDC.SelectObject(&compatBitmap);

compatDC.Rectangle(0, 0, bitmapRect.Width() - 1, bitmapRect.Height() - 1);
// put your other drawing code here

// Open the clipboard
if (OpenClipboard())
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, compatBitmap.Detach()); // clipboard now owns the object
CloseClipboard();
}
else
{
AfxMessageBox("ERROR : Another application is using the clipboard.");
compatBitmap.DeleteObject();
}

compatDC.RestoreDC(-1);
compatDC.DeleteDC();
ReleaseDC(pDC);