liew
liew

Reputation: 79

Create device-independent bitmap without allocating memory

This is what I currenty do to load images in my application:

auto b = ::FreeImage_Load(type, path.c_str());
void *bits;

auto hbmp = CreateDIBSection(
    dc,
    FreeImage_GetInfo(bitmap),
    DIB_RGB_COLORS,
    &bits,
    0,
    0
);

std::memcpy(bits, FreeImage_GetBits(b), size);

It works very well, but I'd like to avoid allocating memory twice - ::FreeImage_Load already stores data in a way that's suitable for dib. Is there a way of calling CreateDIBSection that would prevent allocation and would force dib created that way to use buffer provided by me? If not, is there another method that would allow that?

It seems that providing NULL instead of **ppvBits prevents allocation - is there a way to modify buffer address later? I've also tried tinkering with hSection parameter but dibs created that way were incorrect.

Upvotes: 0

Views: 357

Answers (1)

Strive Sun
Strive Sun

Reputation: 6299

I guess I didn't get your idea wrong.

Maybe you can use CreateDIBitmap to convert FreeImage images to HBITMAP.

FIBITMAP *dib = FreeImage_Load(type, path.c_str());
// ...
HBITMAP bitmap = CreateDIBitmap(hDC, FreeImage_GetInfoHeader(dib),

CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS);

// ...
FreeImage_Unload(dib);

Related Link: How do I convert a FreeImage image to a HBITMAP ?

If you want to use CreateDIBSection, you can use a non-null hSection argument and use CreateFileMapping and MapViewOfFileEx() to create the section, the latter's lpBaseAddress should point to your bits.

Remember to fully initialize the parameters in MapViewOfFileEx and CreateFileMapping.

Here is a similar discussion you can refer: How to create an HBITMAP wrapping an existing byte buffer?

Upvotes: 0

Related Questions