Page 1 of 1

Visual C++ 2005 won't load images from file

Posted: Thu Nov 08, 2007 7:41 pm
by forrestcupp
I'm really getting annoyed with this problem. I'm using Visual C++ 2005 Express. Every time I try to load an icon or bitmap from a file, it won't allow it. It keeps telling me to check my resources. It seems as if it will only accept embedded media without allowing the loading of my media from the hard drive. I really don't want to have to add all of my content to my resources, and I can't even get that to work with a simple png file anyway.

Does anyone have any ideas on what I need to change so that I will be able to load image files?

Posted: Thu Nov 08, 2007 7:52 pm
by tierra
Yeah, you need to change the line of code that loads the image. There are several different ways of loading images, with shortcuts for each. The one you're using is meant only to be used with attached resources.

It'd help if you mentioned how you're loading the images if you expect help with this.

Posted: Thu Nov 08, 2007 7:54 pm
by forrestcupp
In my frame constructor, I have this:

wxString dir = wxGetCwd() + "\\exit.png";
wxBitmap quit(dir);


I also tried:

wxBitmap quit;
quit.LoadFile(dir);

Neither one worked.
I've tried using wxImage, which seems to allow me to load a file, but it keeps saying "No handler found for image type" no matter what type I use.

Posted: Thu Nov 08, 2007 8:17 pm
by timg
For the image handler issue, did you initialize the image handler for the type you want to use?

you could stick this:

Code: Select all

    ::wxInitAllImageHandlers();
into your wxApp::OnInit() function if you didn't.

Posted: Thu Nov 08, 2007 8:26 pm
by forrestcupp
Thank you. I didn't know to do that. I'm just starting to learn this and I'm going through the intermediate tutorial on making a toolbar. When I type the code how they have it, it doesn't work.

So I initialized all image handlers. Then I had to load the file into a wxImage and send that to a wxBitmap to use in the toolbar.

Thank you. I've found that the intermediate tutorials are more confusing than they need to be and they don't tell you everything you need to know. I wish there were a more complete tutorial somewhere that is easy to follow.

Posted: Thu Nov 08, 2007 8:44 pm
by forrestcupp
Well, I have another problem after I already marked it solved.

I can get the image into the toolbar even without converting it to a wxBitmap. But the image isn't shrunk to fit the toolbar. It only shows the upper right corner of the image. Do you know how to get it to be sized to the toolbar? It seems like that if I resize my image to fit my toolbar, it may not be right on another platform.

Posted: Thu Nov 08, 2007 9:17 pm
by timg
I have not used Toolbars, but I would think that you need to resize the image to the correct dimensions. If you are worried about resizing to the right size, call GetToolBitmapSize() on the toolbar and resize to the size it gives you.

The wxImage size can be changed with image.Rescale(width, height)

Posted: Thu Nov 08, 2007 9:45 pm
by forrestcupp
Thank you. The GetToolBitmapSize() function was exactly what I was looking for for portability. I've noticed that these toolbar icons are a lot bigger on some platforms than they are in Windows.