Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How can I load a System Resource image into a PictureBox with the CLR?

 

(This is actually C++ code, but I'm not sure if there is a C++ tag, so I borrowed the CS tag)

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 can't seem to find any way of loading one of the Resource images.

--

Product: Visual C++ 2005 Express

Posted

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:

ResourceManager^ resourceManager = gcnew ResourceManager("MyCompany.MyProject.SomeResources", GetType().Assembly);
Bitmap^ image = (Bitmap^)resourceManager->GetObject ("MyBitmapName");

I haven't specified "MyCompany" or "MyProject" anywhere, so what would I put there? I have tried using my namespace, but it doesn't seem to like that either.

  • Leaders
Posted
My company would be whatever the assembly's company is set to. I don't 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).
[sIGPIC]e[/sIGPIC]
Posted

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 don't 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?

Posted

Wow! I wasn't even aware of that little tool.

.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):

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?

  • Administrators
Posted

If that is the only entry then it looks as though the image isn't 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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...