Displaying Images with Visual C++

Started by
2 comments, last by n8hanson 16 years, 11 months ago
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!
Advertisement
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.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Instead of using Image::FromFile use Bitmap::FromResource.
.
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]

This topic is closed to new replies.

Advertisement