Joshua Vega
Joshua Vega

Reputation: 599

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.

Upvotes: 3

Views: 5060

Answers (2)

VZ.
VZ.

Reputation: 22678

First of all, to avoid problems deep inside wxToolBar, always check the return code of LoadFile() or, alternatively, use wxBitmap::IsOk() to check that the bitmap was successfully loaded.

Second, while adding the handler explicitly as you did is perfectly fine, I'd recommend to just call wxInitAllImageHandlers() as it's simpler and has no real drawbacks unless you are looking to create the smallest program possible.

Finally, to address your real problem, the file clearly doesn't exist at the path you're loading it from. You can, of course, solve this by being careful not to change your working directly (or restore it after changing it) in your program and by placing the file in the correct place. But this is, as you discovered, error-prone, so a better idea is to always use full paths to your resources. To construct them, you will find wxStandardPaths useful, in particular its GetResourcesDir() method.

Upvotes: 3

ravenspoint
ravenspoint

Reputation: 20457

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.

Upvotes: 5

Related Questions