How to get a GDI+ Bitmap from a BITMAP struct?

How to get a GDI+ Bitmap from a BITMAP struct?

Post by fade » Tue, 19 Dec 2006 03:10:04


I've got a BITMAP struct filled with data and I can show the image using:

BITMAP image;
(...)
HDC hdc = GetDC(NULL);
SetDIBitsToDevice(hdc, 600, 500, image.bmWidth, image.bmHeight, 0, 0, 0,
image.bmHeight,
image.bmBits, (BITMAPINFO*)&pVih->bmiHeader,
DIB_RGB_COLORS);
ReleaseDC(NULL, hdc);

The problem arises when I try to convert/save the image.
I've tried different approaches but none of them seems to work so far.

1)
CBitmap cbm;
bool b = cbm.CreateBitmapIndirect(&image); //Returns 0 - FALSE :(

2)
CBitmap cbm;
cbm.SetBitmapBits(bufferSize, pBuffer);
cbm.SetBitmapDimension((int)image.bmWidth, (int)
image.bmHeight); //Crashes
//cbm.SetBitmapDimension(640, 480); ---> crashes also

3)
ULONG_PTR gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;
VERIFY(GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL )
== Ok );

BITMAPINFO bmInfo;
ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
CopyMemory(&bmInfo.bmiHeader, &(pVih->bmiHeader), sizeof
(BITMAPINFOHEADER));

Bitmap* bm = new Bitmap(&bmInfo, (void*)pBuffer);
/* Gives the following error - Gdiplus::GdiplusBase::operator new' :
function does not take 3 arguments */ --- 3 args?????

4)
(...)
CBitmap cbm;
cbm.SetBitmapBits(bufferSize, pBuffer);
HBITMAP hbm = (HBITMAP)cbm;
Bitmap* bm = Bitmap::FromHBITMAP(hbm, NULL);
bm->Save(L"C:\\test.bmp", &Gdiplus::ImageFormatBMP); /* It's
doesn't crash but it doesn't write a file either */

How can I get a working GDI+ bitmap from the data I have?
Thanks in advance

--
fade
email: fade(*AT*)forward(*dot*)to
 
 
 

How to get a GDI+ Bitmap from a BITMAP struct?

Post by Michael Ph » Tue, 19 Dec 2006 05:19:08

Try using the static gdiplus method Bitmap::FromBITMAPINFO.

You pass in a pointer to your bitmap's BITMAPINFO stucture and a pointer to
the bitmap's bits.

 
 
 

How to get a GDI+ Bitmap from a BITMAP struct?

Post by fade » Wed, 20 Dec 2006 10:09:06

"Michael Phillips, Jr." < XXXX@XXXXX.COM > wrote in



Thanks a lot Michael. It works great ;)
--
fade
email: fade(*AT*)forward(*dot*)to