#1,109 – Accessing a Resource in a Different Assembly

You can use the following URI, in code, to access a resource embedded into the executing assembly.

                    BitmapImage bmi = new BitmapImage(new Uri("pack://application:,,,/Images/Ted.jpg"));

If you want to access a resource in a different assembly, you use the Uri syntax shown below, adding the assembly name and the word “component”.

                    BitmapImage bmi = new BitmapImage(new Uri("pack://application:,,,/MyPictures;component/Ted.jpg"));

This assumes that the image Ted.jpg is included in an assembly named MyPictures (e.g. in MyPictures.dll).  It also assumes that the Build Action for the image is set to Resource and that this assembly is in the same directory as the executing assembly.  If you want the resource assembly to end up in the same output directory as the main assembly, you’ll want to add a reference to it from the main assembly.


#831 – Embedding a Cursor in Your Project as a Resource

If you have a custom cursor that your application uses, you can load it at run-time from an external file.  But this requires that you distribute the cursor file with your application.  To avoid distributing the .cur file, you can embed this file into your project as a resource.

First, add the .cur file to your project and set its Build Action to Resource.





To load the cursor at run-time, you use the Application.GetResourceStream method, which returns a StreamResourceInfo object.  You can then use the Stream property of this object to create the cursor.  (You’ll need a using statement for the System.Windows.Resources namespace).

        private void Button_Click_1(object sender, RoutedEventArgs e)
            StreamResourceInfo sriCurs = Application.GetResourceStream(
                new Uri("captain-america-arrow.cur", UriKind.Relative));

            this.Cursor = new Cursor(sriCurs.Stream);


#550 – Converting a Brush into a Resource in Blend

Once you spend some time creating a Brush in Blend, e.g. specifying the different colors in a gradient brush, it’s likely that you’ll want to use the same brush in several different spots in your user interface.  You could just copy/paste the definition of the brush in XAML.  But the easier way to reuse a brush is to convert it to a resource and then to reference the resource by name wherever you use it.

To start with, assume that we have a linear gradient brush defined for a Label control’s Background.

Start by clicking on the white square (Advanced Options) to the right of the brush-related property.

On the menu that comes up, select Convert to New Resource.

Specify a name for the new resource.

Notice that the XAML now defines the brush as a resource and then references it from the Label.

#274 – Embed Images into Your Project as Binary Resources

The easiest way to specify a path to an image to be used in an Image control is to do the following:

  • Embed the image as a binary resource in your project
  • Set the Build Action of the image to Resource
  • Just use the filename as the image’s Source
  • Do not distribute the image with your executable (it’s embedded)

These steps are shown below.

Add the .png file to the project.

Verify that the Build Action is set to Resource.

Just use the filename in the Image tag.

	<Image Source="TractorSm.png"/>

Do not copy the .png file to the output directory.

Here is the final result, displaying the image in a window: