joe_pool_is Posted June 30, 2006 Posted June 30, 2006 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 Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted June 30, 2006 Administrators Posted June 30, 2006 IIRC you need to call the GetManifestResourceStream on an instance of an assembly. Try GetExecutingAssembly() to return the current assembly. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted June 30, 2006 Author Posted June 30, 2006 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. Quote Avoid Sears Home Improvement
Leaders snarfblam Posted June 30, 2006 Leaders Posted June 30, 2006 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). Quote [sIGPIC]e[/sIGPIC]
Gill Bates Posted July 4, 2006 Posted July 4, 2006 Also, the image needs to be specified as "Embedded Resource". Quote
joe_pool_is Posted July 5, 2006 Author Posted July 5, 2006 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? Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted July 5, 2006 Administrators Posted July 5, 2006 If you open the assembly in ildasm and look at the manifest do you see some lines that begin with .mresource public if the resource is being embedded at least one instance should be present. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted July 5, 2006 Author Posted July 5, 2006 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? Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted July 6, 2006 Administrators Posted July 6, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.