How can I load PNG files from Resource? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

How can I load PNG files from Resource?

Post by purplex88 »

I am on Windows using MSVC++14.0 with wxWidgets 3.1.

What I am currently doing is loading PNG files from a path:

Code: Select all

wxBitmap BitmapSettings("D:\\Settings.png", wxBITMAP_TYPE_PNG);

And setting them on a wxButton and achieving transparency:

Code: Select all

m_settings->SetBitmap(BitmapSettings);

// For transparency
m_settings->SetBackgroundColour(GetBackgroundColour());
So, I want to load PNG from a resource instead of a path. As I recall some years ago we had no support for loading a png file from resource file. Is it possible to easily do this now?
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How can I load PNG files from Resource?

Post by doublemax »

Use the source, Luke!
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: How can I load PNG files from Resource?

Post by purplex88 »

Hi, I tried:

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("#107"));
Where "#107" is resource ID of PNG.

Is it the same way I load Icons i.e. wxIcon("#101") ?
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How can I load PNG files from Resource?

Post by doublemax »

I've never used these macros. Try without the #.
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: How can I load PNG files from Resource?

Post by purplex88 »

I think I tried everything but same error:

This is resource.h file
#define IDI_ICON1 102
#define IDB_PNG1 107
#define IDB_PNG2 108
#define IDB_PNG3 109
This is .rc file
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON "icon1.ico"

/////////////////////////////////////////////////////////////////////////////
//
// PNG
//

IDB_PNG1 PNG "Settings1.png"
IDB_PNG2 PNG "Settings2.png"
IDB_PNG3 PNG "Settings3.png"

So I tried the following:

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("#107"));
or,

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("107"));
or,

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("Settings1.png"));
or,

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("Settings1"));
or,

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG("IDB_PNG1"));
or,

Code: Select all

wxBitmap BitmapSettings(wxBITMAP_PNG(IDB_PNG1));
None of these work and I get this error:
error.png
error.png (5.88 KiB) Viewed 5024 times
Insanity.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How can I load PNG files from Resource?

Post by doublemax »

This worked for me:

Code: Select all

// .rc
SOMEPNG RCDATA "toucan.png"

// code
::wxInitAllImageHandlers();
wxBitmap bmp("SOMEPNG", wxBITMAP_TYPE_PNG_RESOURCE);
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: How can I load PNG files from Resource?

Post by purplex88 »

Yes, I also changed "PNG" to "RCDATA":

Code: Select all

IDB_PNG1 RCDATA "Settings1.png"
IDB_PNG2 RCDATA "Settings2.png"
IDB_PNG3 RCDATA "Settings3.png"
and:

Code: Select all

wxInitAllImageHandlers();
wxBitmap BitmapSettings("IDB_PNG1", wxBITMAP_TYPE_PNG_RESOURCE);
I rebuilt the project and it still won't work and ends up with same error.
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: How can I load PNG files from Resource?

Post by purplex88 »

Interestingly, it works only if I add the number ID with '#' with "RCDATA" string.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: How can I load PNG files from Resource?

Post by PB »

Are you sure it is interesting and not by design, i.e., the difference between ordinal and string resource identifiers?
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: How can I load PNG files from Resource?

Post by purplex88 »

I am not sure why the method specified by doublemax won't work. I just haven't seen any tutorial where they use # with number identifier from resource.h.

Edit: Okay, I figured that out as well. Doublemax's method will work if I delete the IDs from resource.h file.
User avatar
Asimov
Earned some good credits
Earned some good credits
Posts: 146
Joined: Tue Nov 17, 2020 6:43 pm

Re: How can I load PNG files from Resource?

Post by Asimov »

Hey I just got my icons to load from a resource file in Codeblocks using the same technique and I haven't even got a resource.h file.
I converted all my pngs to bin using Resource Hacker, saved them out and then just added the following lines to my resource.rc file.

Code: Select all

aaaa ICON "wx/msw/std.ico"

#include "wx/msw/wx.rc"

ARROW-DOWN RCDATA "rc/ARROW-DOWN.bin"

ARROW-UP RCDATA "rc/ARROW-UP.bin"

CREATE RCDATA "rc/CREATE.bin"

DELETE RCDATA "rc/DELETE.bin"

EDIT RCDATA "rc/EDIT.bin"
Now all I gotta do is work out how to load a font from the resource file LOL.

regards
Asimov
Post Reply