Loading an Image from Resource

joe_pool_is

Well-known member
Joined
Jan 18, 2004
Messages
451
Location
Texas
How can I load a System Resource image into a PictureBox with the CLR?

(This is actually C++ code, but Im not sure if there is a C++ tag, so I borrowed the CS tag)
C#:
System::Windows::Forms::PictureBox^ PictureBox1;
System::IO::Stream^ resource;
 
System::Void Button_Click(System::Object^ sender, System::EventArgs^ e)
{
resource = Assembly::GetManifestResourceStream("MyNamespace.Fido.jpg");
// Error is
// "error C2352:
// System::Reflection::Assembly::GetManifestResourceStream :
// illegal call of non-static member function"
PictureBox1->Image = gcnew 
Image::FromStream(Assembly::GetManifestResourceStream("MyNamespace.Fido.jpg"));
// Error is
// "error C2061:
// syntax error : identifier FromStream"
System::Reflection::Assembly^ ExeAss;
ExeAss = Assembly::GetExecutingAssembly();
Bitmap^ image = gcnew Bitmap(ExeAss->GetManifestResourceStream("MyNamespace.Fido.jpg"));
// image is always null
PictureBox1->Image = image;
}
I cant seem to find any way of loading one of the Resource images.
--
Product: Visual C++ 2005 Express
 
IIRC you need to call the GetManifestResourceStream on an instance of an assembly. Try GetExecutingAssembly() to return the current assembly.
 
I tried that with the last group of commands (see ExeAss).

Did I use it incorrectly?

I found some info in the Help about ResourceManager, but I can not seem to find out what it wants for the String parameter:
C#:
ResourceManager^ resourceManager = gcnew ResourceManager("MyCompany.MyProject.SomeResources", GetType().Assembly);
Bitmap^ image = (Bitmap^)resourceManager->GetObject ("MyBitmapName");
I havent specified "MyCompany" or "MyProject" anywhere, so what would I put there? I have tried using my namespace, but it doesnt seem to like that either.
 
My company would be whatever the assemblys company is set to. I dont know about c++, but in C# and VB there is a designer that lets you set these values (in code they are in the form of attributes applied to the assembly).
 
Bill,
In C++ Express, I can bring up the Properties page for each image that is listed under my resources folder of the project, but it only gives me 2 options: One is "Excluded From Build" which I have set to No (because I want it included with the build). The Second is "Tool" where my options are "C/C++ Compiler Tool," "Custom Build Tool" (which is selected by default), "MIDL Tool," "Resource Compiler Tool," "Managed Resource Compiler" (which I tried, but the project could not compile because of errors writing the files), "Web Services Proxy Generator," and "XML Data Proxy Generator." I dont know which to pick or how I should pick it. Any ideas?

Marble,
I have looked in the typical locations for the Assembly properties (as found in VB and C#), but this is not found in VC++. This is hard to figure out.

Any other thoughts?
 
If you open the assembly in ildasm and look at the manifest do you see some lines that begin with
Code:
.mresource public
if the resource is being embedded at least one instance should be present.
 
Wow! I wasnt even aware of that little tool.
Code:
.mresource public Test1.Form1.resources
{
// Offset: 0x00000000 Length: 0x00045182
}
Does this mean "MyCompany.MyProject.Fido.jpg" should be of the form "Test1.Form1?"

I made the mods below (C++ code):
C#:
System::Reflection::Assembly^ ExeAss;
System::Resources::ResourceManager^ ResMan;
ExeAss = Assembly::GetExecutingAssembly();
ResMan = gcnew System::Resources::ResourceManager("Test1.Form1", ExeAss);
System::Object^ objPic = ResMan->GetObject("Fido.jpg"); // does not assign
pbBanner->Image = (Bitmap^)objPic;
The objPic is never assigned the image; it remains null.

Any ideas?
 
If that is the only entry then it looks as though the image isnt being embedded correctly, the entry you have will be fore the images etc. used by the form itself.

Try changing the options discussed above and keep checking with ildasm to see if something extra appears.
 
Back
Top