Article C0011 C# .NET
Embedded Resource(s)

If you don't want to ship your artwork along with your executable, it is possible to embed them into your assembly.

The first step to take is to actually add the resource(s) into the assembly. To perform this task, select the context menu of the project within the Solution Explorer (if the Solution Explorer is not visible press Ctrl+W,S) and select 'Add'-'Existing Item(s)'. You probably have to change the extension filter to match the extension of the resource.

The next step is extremly important and is forgotten is most occasions. Select the previously added resource(s) and change the Build Action within the Properties area from Content into Embedded Resource.

Resource Image
As an example, place a PictureBox onto a Windows Form and call it 'img'. After the image resource(s) are added and the PictureBox is available the PictureBox can be filled using the following code:

    Assembly asm = Assembly.GetExecutingAssembly();
        img.Image = Image.FromStream(
Don't forget to add a reference towards the Reflection library:

using System.Reflection;

The name "Resource" in the previous shown code is the name (namespace) of our assembly (application).

If the resource doesn't exist within the assembly an ArgumentException with a value of null within the stream will be shown.

The next item to embed is a sound file. In our example, which can be downloaded below, we have used a sample using the regular WAVE (.WAV) sound format.

To be able to play a soundfile, add a reference towards the Media library:

using System.Media;

A sound sample can be played using a soundplayer object. You can simply declare one like this:

SoundPlayer snd_cash;

Next the sample can be loaded using the following code:

snd_cash = new SoundPlayer(
If you want to play the sound sample, simply call the .Play() method like this:

The example which can be downloaded here, shows the use of both of these resources. The application looks like this:

Resource Form
and each button will show legacy money used within the Netherlands. When a button is pressed, the money picture changes and the sound of a cashregister is played. The example contains the full sourcecode (of course).