Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few different options when it comes to retrieving binary resources in WPF, and they all depend on which kind of behavior you want.</p> <ol> <li><p>If your resources should be embedded inside an assembly, add them to the project in Visual Studio, and set the <em>Build Action</em> to <em>Resource</em>. This way they will be baked into the assembly, and therefore can't be changed easily.</p></li> <li><p>If they should be left as loose files, add them to the project, and set the <em>Build Action</em> to <em>Content</em>. Also make sure they are being copied to the output directory. This is a good idea, if you need to replace them frequently, and don't feel like recompiling the assembly every time.</p></li> <li><p>If you want them as loose files, but don't want to include them in your Visual Studio solution for some reason (maybe they are not known at compiletime), you can access them using the full path or with something called SiteOfOrigin notation. I won't go into this as that is not relevant in your case.</p></li> </ol> <p>To access the resources from your code you use a Pack URI, which can have different forms:</p> <ul> <li><p><code>pack://application:,,,/img.jpg</code><br> References image in the root of the project.</p></li> <li><p><code>pack://application:,,,/Folder1/Folder2/img.jpg</code><br> References image in a subfolder of the project.</p></li> <li><p><code>pack://application:,,,/NameOfDll;Component/img.jpg</code><br> References image in a different assembly to which there is a reference in Visual Studio.</p></li> </ul> <p>Luckily there is no need to write the full URI when a resource is referenced from XAML.<br> Basically the <code>pack://application:,,,</code> part can be avoided, because a TypeConverter exists that can translate part of the location to the full URI for you.</p> <p>For points 1 and 2 above, the same XAML is used to reference the resources, regardless of whether they exist as loose files alongside the assembly at execution time or are embedded inside of it.</p> <p>The fully defined explicit URI has to be used when referencing the resources from procedural code, though.</p> <p>I have written some code and included some images, to show how this works.</p> <p><img src="https://i.stack.imgur.com/H77OL.png" alt="Image showing running program"> <img src="https://i.stack.imgur.com/EDQnQ.png" alt="Image of solution explorer view"></p> <p>Relevant XAML:</p> <pre><code>&lt;StackPanel&gt; &lt;TextBlock Text="Embedded in same assembly" /&gt; &lt;Image Source="gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Embedded in same assembly in a subfolder" /&gt; &lt;Image Source="Content/Images/gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Embedded in same assembly in a subfolder using full pack URI format" /&gt; &lt;Image Source="pack://application:,,,/Content/Images/gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Embedded in different assembly" /&gt; &lt;Image Source="/Module1;Component/gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Embedded in different assembly in a subfolder" /&gt; &lt;Image Source="/Module1;Component/Images/gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Embedded in different assembly in a subfolder using full Pack URI format" /&gt; &lt;Image Source="pack://application:,,,/Module1;Component/Images/gift.png" /&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Setting imagesource from code-behind" /&gt; &lt;Image x:Name="image1" /&gt; &lt;/StackPanel&gt; </code></pre> <p>Relevant code-behind:</p> <pre><code>public Window1() { InitializeComponent(); //Here we have to use the full Pack URI //image1.Source = new BitmapImage(new Uri("/Module1;Component/Images/gift.png")); //Throws exception.. image1.Source = new BitmapImage(new Uri("pack://application:,,,/Module1;Component/Images/gift.png")); } </code></pre> <p><strong>Update</strong><br> When the resource is in the same assembly that is using it, there should be no reason to include the <code>/NameOfDll;Component/</code> part of the URI. I'm not sure why it's not working in your case.</p> <p>I have added a Window to Module1, that just references a single image in its own assembly, and that seems to work fine. The Window is shown when a button is clicked in the WPF application.</p> <pre><code>&lt;Window x:Class="Module1.WindowTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="Window from Module1"&gt; &lt;Grid&gt; &lt;Image Source="Images/gift.png" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><img src="https://i.stack.imgur.com/1A4Ub.png" alt="Showing different window with resource shown"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload