0

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.

liew
  • 69
  • 1
  • 9
  • `hSection` is the way to do it. What do you mean they were incorrect? – Jonathan Potter Jun 21 '19 at 02:31
  • @JonathanPotter OK, thank you, I'll try again in such case. I couldn't display them on my hdc. Maybe I've used `CreateFileMappingA` incorrectly? – liew Jun 21 '19 at 04:34
  • What is the purpose of creating device-independent bitmap `hbmp`? – Strive Sun Jun 21 '19 at 05:44
  • @StriveSun-MSFT ddbs have some limitations, e.g. max size is limited, it also has to got the same depth as device you're going to display it on. But the best thing about dibs is that you can freely edit their buffers – liew Jun 21 '19 at 09:39
  • Yes, if you need different depths, only use `CreateDIBSection`. You can refer to another link. – Strive Sun Jun 21 '19 at 09:47

1 Answers1

0

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?

Strive Sun
  • 5,988
  • 1
  • 9
  • 26