Click to See Complete Forum and Search --> : Copying HBITMAP to clipboard as CF_DIB


aewarnick
June 11th, 2005, 11:31 AM
aBmp* b= setts.imArr[imList.SelInd()]->bmp;
ushort sizeofBmpInfoHead= sizeof(BITMAPINFOHEADER);
BITMAPINFO bi;
memset( & bi.bmiHeader, 0, sizeofBmpInfoHead);
bi.bmiHeader.biSize= sizeofBmpInfoHead;
bi.bmiHeader.biWidth= b->W;
bi.bmiHeader.biHeight= b->H;
bi.bmiHeader.biPlanes= 1;
bi.bmiHeader.biBitCount= b->bpp;
uint sz= sizeof(bi)+b->WBits*b->H;
char* c=new char[sz];
memcpy(c, &bi, sizeof(bi));
memcpy(c+sizeof(bi), b->pBits, b->WBits*b->H);
CB::ToCB_Bmp(Hwnd, c, sz);
delete[]c;

//...more code...

::SetClipboardData(CF_DIB, hGlobal);
Did I do something wrong there. This works but the colors are all messed up and the images are 24 bit.

NoHero
June 11th, 2005, 11:36 AM
Maybe this (http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1715/) article helps you.

aewarnick
June 11th, 2005, 12:13 PM
Nope, had already come accross that article. It uses MFC so everything important is hidden and I don't use MFC. So it's useless to me. Thanks for trying.

NoHero
June 11th, 2005, 12:24 PM
Can you provide sample code that reproduces this error so we can test it? Or post your project? Or your code in the way we can easily use it...?

aewarnick
June 11th, 2005, 12:29 PM
It's not an error. The colors of the image are just shifted. I'm missing something in the code posted above. The shift takes place somewhere in here:
ushort sizeofBmpInfoHead= sizeof(BITMAPINFOHEADER);
BITMAPINFO bi;
memset( & bi.bmiHeader, 0, sizeofBmpInfoHead);
bi.bmiHeader.biSize= sizeofBmpInfoHead;
bi.bmiHeader.biWidth= b->W;
bi.bmiHeader.biHeight= b->H;
bi.bmiHeader.biPlanes= 1;
bi.bmiHeader.biBitCount= b->bpp;
uint sz= sizeof(bi)+b->WBits*b->H;
char* c=new char[sz];
memcpy(c, &bi, sizeof(bi));
memcpy(c+sizeof(bi), b->pBits, b->WBits*b->H);I'm probably missing a sizeof or something because my aBmp class is ok.