Hi All,
I'm trying to figure out how to load images from a resource file into a picture box using a visual studio windows forms application in c++. I figured out two different ways to do this in c# by going through the forums, but I cannot figure it out for c++. Here is an example of the working code in C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.IO;

namespace load_resource_image
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            this.pictureBox1.Image = Properties.Resources.another_image;


        private void button2_Click(object sender, EventArgs e)
            System.IO.Stream stream;
            System.Reflection.Assembly assembly;
            Image bitmap;

            assembly = System.Reflection.Assembly.LoadFrom(Application.ExecutablePath);
            stream = assembly.GetManifestResourceStream("load_resource_image.Resources.my_image2.png");
            bitmap = Image.FromStream(stream);
            this.pictureBox1.Image = bitmap;

For the first one, (button click 1) I had to add a picture into the Resources.resx file in the properties folder. For the second one I added an image called my_image2.png to the resources folder, then set the build action to embedded resource in the properties window. I tried a bunch of things that did not work in C++, and searched through forums, but was not able to find anything that works.

See http://www.windows-tech.info/18/504d408117484041.php for a great reference.

Normally in C# I would add in another .resx file but there doesn't seem to be a way to do that in 2008 or 2010 Express Editions. You can add them to the existing Form1.resx (the IDE will balk at you): open that file up in one of the tabs (it may be hidden under Form1.h in the solution explorer) and go to the add resource. Note the name that VS gives your image for later.

Follow the steps in the link above, except instead of Project1.Form1 substitute <YourNameSpace>.Form1. Skip over the GetStream step and instead do pictureBox1->BackgroundImage = dynamic_cast<Image^>(myres->GetObject("thenameVSgaveyourresource")); Give that a try...

Thanks for the help. One problem is that when the form designer is opened back up, it erases all of my added resources, which is really painful since I have so many items I want to load dynamcally. I actually went down this path before, using an array like this:

imageArray = gcnew array<Image^>(2);
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
array<String^>^ names = {"image1.png","image2.png"};

this->pictureBox1->Image = imageArray[1];

I also found that I can create my own resx file and add my images. The only problem is that I don't know how to access images from my own resx file. The file is called my_resources.resx. Do you know how to access images in this file?

How did you add the resx file? (I couldn't do it in the Express Edition). The resource I added to Form1.resx did persist there after I reopened it.

If you go to access the resources in C# you just use my_resources.resourcename but as we've figured out it's a slightly different animal in C++/CLI.

In the solution explorer, I click on the project name, then add new item, click resource, and it shows me Assembly Resource File.(resx) along with (rc), (bmp), etc.

I think I have the pro version of visual studio installed. It sounds like I need a copy of express. It's surprising that there is a button for add resource under form1.resx when it just deletes the resources I add. In fairness, it does warn me about editing the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.