GDI+ Image to HBITMAP

2.3k views Asked by At

I have the following code:

Image *img=Image::FromFile(currentImagePath.c_str(), false);


Bitmap* pBitmap = static_cast<Bitmap*>(img/*->Clone()*/);
HBITMAP handleToSliceRet=NULL;
Status status = pBitmap->GetHBITMAP(Color(0,0,0), &handleToSliceRet);

I have initialized gdi and everything, but when I try to run this, if I uncomment the /->Clone()/ the debugger will say Access violation reading 0x0000 and if I leave it commented, it fails on the line Status status = pBitmap->GetHBITMAP(Color(0,0,0), &handleToSliceRet); with another access violation. I've been at this for about 6 hours now to no avail.

The current path of the image I'm trying to process is "C:\cube\cube0000.png"

1

There are 1 answers

4
Jonathan Potter On

Bitmap derives from Image, not the other way around. Image::FromFile returns an Image, not a Bitmap - and you can't just cast it into existence.

Instead, use Bitmap::FromFile from the beginning to get a real Bitmap object.