From Form to DLL - How Do I Package Resource Icons and Files?
-
I began with a textbook Form example (Petzold's Directory TreeView Form)that uses icon resources and modified/experimented with it to begin the C# learning process. There are icon resource files used in the project and they have their property "Build Action" set to "Embedded Resource". My attempts at placing the icons in their own folder failed. It seems they must be in the solution's root directory? I suspect this observation is because: "I don't know diddly yet!" I am now trying to transform my modified Petzold code into a DLL. I need to learn how to wrap up the resources so that they are contained in the DLL. I've been able to create and use the DLL ... but only when I "copy" the icon resource files into the \bin\release directory. :( What are the steps I should take so that the resources become a part of the single *.dll file? When I double click the "Properties" folder in the SolutionExplorer and then select "Resources" ... I am prompted to create a default resources file. Here I can add Icon files and text files. Awesome if that was all that was needed to have the resources become part of the DLL file!! But my DLL does not "find" those resources. :(( Again, I seem to have to copy them into the \bin\release directory for them to be found. Please teach me or point me towards a good tutorial or textbook that covers the usage of resources in a DLL. Thanks for all help and suggestions. -- Tom
-
I began with a textbook Form example (Petzold's Directory TreeView Form)that uses icon resources and modified/experimented with it to begin the C# learning process. There are icon resource files used in the project and they have their property "Build Action" set to "Embedded Resource". My attempts at placing the icons in their own folder failed. It seems they must be in the solution's root directory? I suspect this observation is because: "I don't know diddly yet!" I am now trying to transform my modified Petzold code into a DLL. I need to learn how to wrap up the resources so that they are contained in the DLL. I've been able to create and use the DLL ... but only when I "copy" the icon resource files into the \bin\release directory. :( What are the steps I should take so that the resources become a part of the single *.dll file? When I double click the "Properties" folder in the SolutionExplorer and then select "Resources" ... I am prompted to create a default resources file. Here I can add Icon files and text files. Awesome if that was all that was needed to have the resources become part of the DLL file!! But my DLL does not "find" those resources. :(( Again, I seem to have to copy them into the \bin\release directory for them to be found. Please teach me or point me towards a good tutorial or textbook that covers the usage of resources in a DLL. Thanks for all help and suggestions. -- Tom
If you add a resource file controled by .net you can access the images in code using the resource file name. For example if you add an image (MyImage.jpg) to a resource file (MyResources.resx) you will be able to access it like below (check your compiler warnings and click enable strongly typed resources if you see it).
Image myImage = MyResource.MyImage;
However, I suspect you may be having problems with the namespaces of your code. To access resources you must correctly know the namespace. You can embed resources just like you mentioned by changing the build options in properties. If you do this you would need to do the following to access the image. Note that the resource name includes the full namespace which will by default include any sub folders used to store the image.Assembly assemblyExecuting; Stream streamImage; Image imageMine assemblyExecuting = Assembly.GetExecutingAssembly(); streamImage = assemblyExecuting.GetManifestResourceStream("MyNameSpace.Folder.FileName.jpg"); imageMine = Image.FromStream(streamImage); streamImage.Close();