Sign in to follow this  
n8hanson

Displaying Images with Visual C++

Recommended Posts

I'm writing a "Virtual Pet" program where you have a series of buttons that affect your virtual pet's attributes. For example, if you push the KICK PET button, your pet's happiness goes down by 5. As the pet's happiness changes, I want to display a picture (jpeg) that represents the pet's current mood. I have a series of 6 images I'd like to cycle through depending on the current happiness value. My Attempts Using Visual C++: I first tried using a pictureBox control with an imageList (loaded with the 6 jpegs). The program worked, but the images were severely distorted. I then tried using the Image::FromFile method. This also worked as intended, but since the images are not embedded in the code (the program must locate them on the hard drive) this program only works on my machine when my pictures are in the right location. Finally, I have converted a jpeg image into a .RES file (I assume this means the picture will be embedded in the compiled program), but I don't know how to access the file and display it in my pictureBox. Does anybody know how I can accomlish my goal? Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
You almost got it!
I got a slideshow program written in C++ that cycles through various images and I use LoadResource() to load the images that are in resource files.
I would post the code but I find C++ code so ugly nowadays after using Python all the time I can't get myself to post it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mastaba
Instead of using Image::FromFile use Bitmap::FromResource.


Ok I found the code example from Microsoft on using Bitmap::FromResource. Here it is:

public:
static Bitmap^ FromResource (
IntPtr hinstance,
String^ bitmapName
}

EDIT____________________________EDIT

I'm using this command:
pictureBox1->Image = Bitmap::FromResource(IntPtr.Zero, "MyResourceName");

Program runs, but as soon as it tries to load the resource it quits with this error:

"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid."

What am I doing wrong? What can I do to make this work?

[Edited by - n8hanson on May 8, 2007 1:46:09 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this