## Recommended Posts

Nali    131
Need help with C# source code for loading image from embedded resource. I´ve spent too much time today googling/trying to get this to work but every guide is different and seems outdated / fails to work in Visual Studio 2008 Pro which I´m using. I need working code for loading a jpg file from the embedded resource in a program and assign to background of a button. This is the closests attemp so far: using System.IO; using System.Reflection; Assembly _assembly; Stream _imageStream; try { _assembly = Assembly.GetExecutingAssembly(); _imageStream = _assembly.GetManifestResourceStream("Program.picture1.jpg"); } catch { MessageBox.Show("Error accessing resources!"); } // this is where I get the runtime error "value null is not valid for stream" button1.BackgroundImage = new Bitmap(_imageStream); Thanks in advance. Edit: *sighs*, the path in "GetManifestResourceStream" was incorrect.. the picture was in a Resource folder and therefore it should be "Program.Resource.picture1.jpg" .. at least one tutorial helped -_-. Problem solved.

##### Share on other sites
TheTroll    883
System.Reflection.Assembly thisExe;thisExe = System.Reflection.Assembly.GetExecutingAssembly()System.IO.Stream file thisExe.GetManifestResourceStream("Assembly_Name.Image_Name.jpg");this.button1.Image = Image.FromStream(file);

Tada. Just need to make sure you know what the Assemble_Name is and wht the pic name is. If you are having trouble. Do this.

System.Reflection.Assembly thisExe; thisExe = System.Reflection.Assembly.GetExecutingAssembly();string [] resources = thisExe.GetManifestResourceNames();string list = "";// Build the string of resources.foreach (string resource in resources)   list += resource + "\r\n";

That will get you the names of all of your resources.

theTroll

##### Share on other sites
SirViver    136
If it's a local resource, why not access it directly?

button1.BackgroundImage = Properties.Resources.myImageName;

Or if the image is always the same, just set it from the designer.

[Edited by - SirViver on March 31, 2010 8:32:29 AM]

##### Share on other sites
Nali    131
Thanks for the answers, I already solved it shortly after posting .. should have posted the thread much earlier that day :P.

Since I´m new into GUI stuff in C# I find it not to easy to figure out stuff like that.. and the lack of decent tutorials on the net doesnt make it better. Its a good thing gamedev.net exists and that you guys have patience :)

Thanks for the tips, I´ll check it out later on.

##### Share on other sites
Nali    131
Quote:
 Original post by SirViverIf it's a local resource, why not access it directly?*** Source Snippet Removed ***Or if the image is always the same, just set it from the designer.

This did work after some testing, there was no definition for the pictures until I added them from the right place. :S

Pojectname -> Properties -> Resources.resx (rightclick -> Designer) and Add resource.

Just made this clear for others who might encounter the same problem.

Adding resources throu rightclick -> Add Existing Item on the Resource folder in the Project view does not add definition.

EDIT:

Encountered new obstacles, a function returns a string with a picture name from the project resources and I need it to be used in Properties.Resources."var" so I can dynamically get the needed resource. Is there a way to do this?.

string res = GetButtonPictureName();
gameButtons[i].BackgroundImage = Properties.Resources.res // does not work ofc since res is not an identified resource.

[Edited by - Nali on April 1, 2010 10:55:58 AM]

##### Share on other sites
turnpast    1011
When adding an image resource to the resource editor (the one you get when you double click on a resx file in your project) make sure to select images in the resource type dropdown (the first one on the top left by default). You can just drag and drop an image file from explorer or from your project onto the main page of the resource editor. Once it is in the resource editor you should be able to refer to it by its name as SirViver suggested. As long as it is actually a image resource the type of its static member should be Bitmap which is a subclass of image and can be assigned directly to a button's image or whatever.

The ResourceManager.GetObject(name) method will allow you to retrieve a resource given its string name. If it is an image resource you should simply be able to cast its return value to Bitmap or Image.

[code]
button1.BackgroundImage = (Image)Properties.Resources.ResourceManager.GetObject("myImageName");
[code]