Search code examples
c++wxwidgets

WxWidgets - Unable to load images


I recently started working with WxWidgets (2.9.4) and was working through a tutorial I found, but it seems that I'm unable to load any images. I've already properly used the handler (for PNG) and the problem happens at run-time. Below is an image of the popup that is displayed when attempting to run the program.

Run-time error window

Here is the code:

wxPNGHandler *handler = new wxPNGHandler;
wxImage::AddHandler(handler);

wxBitmap exit;

exit.LoadFile(wxT("exit.png"), wxBITMAP_TYPE_PNG);

wxToolBar *toolbar = CreateToolBar();
toolbar->AddTool(wxID_EXIT, exit, wxT("Exit"));
toolbar->Realize();

Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(mainWindow::exitProg));

Any help is appreciated.

EDIT: I forgot to mention that when I click Cancel, this happens:

Second Error

I placed the exit.png file in the build directory (/Debug or /Release) as well as the source code directory, but it still has yet to see it.


Solution

  • What is your working directory?

    If you are using visual studio and running using the interface ( F5 or ctrl-F5 or the little run button in the toolbar ) then your working directory is the folder containing the project file. So try copying your image file there.

    Or open a command window, cd to one of your build directories, and run your app from the command line.

    In general, to avoid this sort of problem, I alter the project properties so that the executable is NOT stored in one of the build folders, but in a new folder ( which I usually call 'bin' - my unix roots are showing! ) and also alter the debugging properties so that the working directory is the bin folder.

    There are a couple of advantages to this technique:

    • Both the release and trhe debug version use the same folder, so you only need one copy of any extra file, like your image file.
    • It is easy to see the executable and extra files in the working directory without being distracted by all the .obj files that end up in the build folders

    IMHO this is well worth the little extra trouble in maintaining non default project properties.