Assembly resources are digital objects, such as images, that aren’t generated by the source code. Assembly resources can be embedded in the executable, or you can supply them to the executable as separate files, also called loose files. Assembly resources were briefly discussed in the post C# XAML Resources Introduction.
To embed a resource in the executable, first add the resource to the Visual Studio project. The easiest way to do this is by right-clicking the project name in the Solution Explorer, selecting the Add, Existing Item… menu selection, and navigating to the item. Visual Studio then adds to the project the item you selected. Next you need to tell Visual Studio to embed the resource in the executable in the form that WPF uses. You do this by setting the Build Action property in the Properties window. To show the Properties window, right-click the resource name (which is now visible in the Solution Explorer), and select the Properties menu item. In the Properties window, select Resource—not Embedded Resource— for the Build Action.
Although it sounds like exactly what you want, make sure you don’t choose Embedded Resource. This option stores the resource in a different part of the assembly that is used by the Windows Forms framework, but it is much more difficult to access from WPF.
For resources that you don’t want embedded in the executable, you don’t have to do anything in Visual Studio as long as you deploy the resource files along with the executable and make sure they’re located where your code thinks they are. Visual Studio, however, can manage that process for you and save you trouble. To takeadvantage of this, you need to add the resource to the project, as you did to embed the resource and then set the Build Action property to Content. The Content setting tells Visual Studio not to embed the resource. Finally, you need to set the Copy to Output Directory property to either “Copy always” or “Copy if newer.”
Accessing Assembly Resources from the Code
Using assembly resources from XAML is easy—you just specify the name of the resource file as a string and assign it to the property where you want to use it. For example, the following line of markup hooks up the Balloons.jpg to the Source property of the Image element: