Resources, where to put them, and how to reference them in C#
Solution 1
You can add a Resources.resx file to your project and add resources like images, strings, files to it. Then you can reference these resources through an automatically-generated Resources
class. By default, Visual Studio will create a Resources.resx file in the Properties directory. Any resources you add to the resources file will be added to the Resources directory by default.
e.g.
this.BackgroundImage = Properties.Resources.MyImage;
Solution 2
For completion I wanted to expound on some of the answers listed.
Embed in Resources.resx
Resources.resx
should have been created when your WinForms project was created. You can view it under Properties
in the Solution Explorer
.
Double click Resources.resx
to open the designer. You can copy from Windows Explorer
and paste in the VS resources designer. This tool is smart enough to figure out what type of resource it is, and you can see in the screen shot below that similar resource types are grouped together under the menu bar's drop down list.
From this point you can follow the accepted answer and manually assign the resource to a control's property...
this.BackgroundImage = Properties.Resources.MyImage;
Embed in form via designer
Using the designer, you can embed a resource in a form's .resx
.
Select the control in the designer and open the Properties
window (F4
is default shortcut key). Find the appropraite property, such as Icon
for a form. Click the ellipses button to bring up the Open File
dialog. Browse to the resource (if you embedded it in the Resources.resx
it will actually be in the Resources
folder - which would have been created when you added your first resource to Resources.resx
- and you should use the first method above), and select the correct resource.
In the image below you can see the "scrape.ico" file being set as the Main
form's Icon
.
This example would end up generating a line in the Main
form's designer like this...
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
where...
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
and in main.resx
...
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
...img data...
</value>
</data>
Solution 3
You can always do it this way.
Go to Project-->Project properties(always name of project + properties)-->Resources
On the upper menu bar you will find a combo box where you will select which type of element you want to use as a resource.
Example: If you need to change the icon for the form(the one that appears an the upper left side of the form) you will then to upload a ".ico" file. If you need to insert a button image you must upload a ".png" file. You can see this Resx files tutorial youtube video where it's explained perfectly
Once you have done this you select the control you wish to insert the image in and go to Properties-->Image and the select the image you have just added from Project Resource file option.
OogaBooga
Updated on July 09, 2022Comments
-
OogaBooga almost 2 years
I've worked with C# and other programming languages for a while now, and am ashamed to say I'm unfamiliar with the standard on where to put resources such as program icons, and how to reference them within my code.
Specifically, for a C# Windows Forms Application, where would it be appropriate to put my icon resources, and what is the path for referencing them once I get them in the correct place?
Thanks very much for any assistance.
-
OogaBooga over 13 yearsYou have an idea on how to reference it in Visual Studio with the GUI once it is places in the Resources.resx file? All of the code is generated automatically and uneditable. :(
-
OogaBooga over 13 yearsAh, I just referenced the location that the icon is automatically placed when you add it to the resources.resx file in the file system. I'm hoping that will still be the correct location when I create a release for this app.
-
Kevin Kibler over 13 yearsIf you add a file to Resources.resx, it will be included in your assembly as an embedded resource. It does not need to be included as a separately file when you release your application.
-
OogaBooga over 13 yearsPerfect. Thanks very much. :)