Using images from resource file 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
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Using images from resource file

Post by parad0x13 »

I have a .rc file as such:

Code: Select all

icon ICON "wxVertigo.ico"
#include "wx/msw/wx.rc"
Splash BMP "Splash.bmp"
and I want to use this function to load a splash screen:

Code: Select all

bool splash = true;
if(splash){
wxBitmap *bitmap = new wxBitmap("Splash.bmp");
wxSplashScreen *splash = new wxSplashScreen(*bitmap,
wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT,
1000, NULL, -1, wxDefaultPosition, wxDefaultSize,
wxSIMPLE_BORDER|wxSTAY_ON_TOP);
wxMilliSleep(1000);}
but when I compile I get the error:

Code: Select all

Can't load bitmap 'Splash.bmp' from resources! Check .rc file.
I don't know what I'm doing wrong! Even if I call:

Code: Select all

wxBitmap *bitmap = new wxBitmap("Splash");
||
wxBitmap *bitmap = new wxBitmap(Splash); <-- Compile error
||
wxBitmap *bitmap = new wxBitmap(Splash.bmp); <-- Compile error
I just cannot load the image for the life of me

It's probably something very simple and stupid on my part that I need to fix but I'm so lost its unfortunate.

Any and all help that can be given in any form will be appreciated very much!

- In advance, Thank You!
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

For your rc declaration you should be doing this

Code: Select all

wxBitmap *bitmap = new wxBitmap("Splash"); 
Are you sure the path is correct and the .rc is compiling and getting linked to the app?

-Max
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

Yes I am sure that it is linking to the executable because I commented

Code: Select all

Splash BMP "Splash.bmp"
out and compiled, then cross checked the file size to that of when it was compiled with the resource and the bitmap is defiantly included in the binary

and I used

Code: Select all

wxBitmap *bitmap = new wxBitmap("Splash");
and I still get the error message from before

But if I use the BITMAP identifier in the resource file it works perfectly:

Code: Select all

Splash BITMAP "Splash.bmp"
Apparently it has something to do with the BMP identifier I used... is there a way that I can make my own identifier that is treated as a BITMAP identifier? I want to use .jpg files too, and I dont know if I can use BITMAP for that
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

Alright, so I can load bitmap(.bmp) files no problem using this technique

resource.rc

Code: Select all

Splash BITMAP "Splash.bmp"
but I want to use .jpg files as the splash picture. I've already initialized all the image handlers too by the way.

when I try this:

Code: Select all

Splash BITMAP "Splash.jpg"
I get an error saying that the image is not 3.00 standard, which makes sense when you imagine that a .jpg is not handled by BITMAP. However I tried this also

Code: Select all

Splash JPEG "Splash.jpg"
and it compiles, but I get that annoying "Cant load image from resource" error that we were talking about early in this thread

I also want to load PNG files etc... so I want to know how to use different image formats loaded from the .rc file in the wxBitmap function

-Please Help!
hilda
Knows some wx things
Knows some wx things
Posts: 49
Joined: Tue Apr 01, 2008 6:22 am
Location: India

Post by hilda »

Code: Select all

wxBitmap bitmap;
bitmap = wxBitmap( "Splash" );
.rc file:

Code: Select all

Splash BITMAP "Splash.bmp"
Last edited by hilda on Mon Mar 16, 2009 6:34 am, edited 2 times in total.
Regards

Windows XP/wxWidget2.4.2
Firebird SQL2.0
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

parad0x13 wrote:I also want to load PNG files etc... so I want to know how to use different image formats loaded from the .rc file in the wxBitmap function

-Please Help!
you can try adding the bitmap type to the bitmap constructor
wxBITMAP_TYPE_BMP Load a Windows bitmap file.
wxBITMAP_TYPE_BMP_RESOURCE Load a Windows bitmap resource from the executable. Windows only.
wxBITMAP_TYPE_PICT_RESOURCE Load a PICT image resource from the executable. Mac OS only.
wxBITMAP_TYPE_GIF Load a GIF bitmap file.
wxBITMAP_TYPE_XBM Load an X bitmap file.
wxBITMAP_TYPE_XPM Load an XPM bitmap file.

But I think that wxBitmap will only handle bitmaps. I have some windows specific code that will load any arbitray file embedded in a .rc that I can try and dig up. I think i posted it here, you can try searching for embedded png or similiar search terms as thats what I think the question was about, but its been a year or so...

Otherwise, I'll see if i can dig up the code from a backup tomorrow, if it'll help.

-Max
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

I found the thread where I posted the code for loading any resource type (any binary file not just images) from an rc.

http://forums.wxwidgets.org/viewtopic.p ... hlight=png

-Max
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 »

I came across this code earlier, but I was hoping that there was some way that you could load PNG or JPG files nativity through the wxBitmap or wxImage functions

If there is then that's what I would like to know how to do, if not then I'm wondering if:

Code: Select all

wxMemoryInputStream *GetResourceInputStream(wxString resource_name, wxString resource_type){
        HRSRC hrsrc=FindResource(wxGetInstance(), resource_name, resource_type);
        if(hrsrc==NULL) return NULL;

        HGLOBAL hglobal=LoadResource(wxGetInstance(), hrsrc);
        if(hglobal==NULL) return NULL;

        void *data=LockResource(hglobal);
        if(data==NULL) return NULL;

        DWORD datalen=SizeofResource(wxGetInstance(), hrsrc);
        if(datalen<1) return NULL;

        return new wxMemoryInputStream(data, datalen);
}

bool HasResource(wxString resource_name, wxString resource_type){
        HRSRC hrsrc=FindResource(wxGetInstance(), resource_name, resource_type);
        if(hrsrc==NULL) return false;

        HGLOBAL hglobal=LoadResource(wxGetInstance(), hrsrc);
        if(hglobal==NULL) return false;

        return true;
}
is really the easiest way of doing it
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

The problem isn't that wxBitmap and wxImage won't load png and jpeg images. They will. The problem is that the images as you are using them are embedded in the app through an rc file. The wxBitmap constructor implicitly loads the binary data using the BITMAP resource type.

You could try adding a png resources as type BITMAP

Code: Select all

MyPng BITMAP "mypng.png"
I don't think this is going to work though.

To be fair though using the functions isn't really that difficult is it? Cut and Paste them somewhere. 1 function call to get it. or 2 if you want to add a check for error reporting. Not really any more complex(after the c&p) than what you are trying, no?

-Max
Post Reply