Search code examples
c++winapiwtl

Is there any way to load a .png from my resources without using GDI+?


I am trying to load a .png I stored in my projects resources, in order to set it in a picture control, but I can't quite figure out why. I did some research, and it seems like .png is not really supported with the usual LoadImage()-call.

However, I don't really want to convert it to a bitmap if I can get around it.

So far, I only found resources on how to do it with GDI+, or really ancient win32-api code.

Is there any way to load .png files natively by todays standard?


Solution

  • The "new" way to do it would be Direct2D and WIC, which is demonstrated in the Windows Imaging Component and Direct2D Image Viewer Win32 Sample.

    But if the rest of your application is basic controls, Direct2D is overkill. The image has to be converted to a bitmap at some point to be displayed – whether in your GPU or in memory – and GDI+ fits this use case.

    If these resources are icons or some other small-ish file (<2mp), I would reccomend embedding the resource as a bitmap. You can keep your asset pipeline as PNG, just add a pre-build step to convert your PNGs to pre-multiplied BGRA bitmaps and use LoadResource. There are pre-built tools to meet this need.