Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Resources.resx to link images

I have included an icon file inside my Resources.resx that I'd like to show on a TreeViewItem that is inside a stackpanel.

1)Can .ico files be used for this purpose? Or does it have to be .bmp or jpg?

2)What do you set the source as in XAML? The following code didn't work for me

<StackPanel Orientation="Horizontal">
    <Image Margin="2" Source="/Resources/Folder_Back.ico" />
    <TextBlock Margin="2" Text="{Binding ProgramName}"
     Foreground="White" FontWeight="Bold"/>
</StackPanel>
like image 760
l46kok Avatar asked Sep 11 '12 06:09

l46kok


2 Answers

The accepted answer says it is not possible and the working solution converts GDI+ Bitmap types to WPF images. But those conversions are completely unnecessary. The solution is actually really simple:

  1. When you add image or icon files to a resource file, the designer picks GDI+ types for them by default:

enter image description here

  1. Just open the .resx file in the XML editor (in Solution Explorer right click, Open With...) and change Bitmap and Icon types to MemoryStream:
<!--<value>..\Resources\Undo.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>-->
<value>..\Resources\Undo.png;System.IO.MemoryStream</value>

...

<!--<value>..\Resources\Error.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>-->
<value>..\Resources\Error.ico;System.IO.MemoryStream</value>

  1. Save the .resx file. If you now open the designer you can find your resources under the Other menu.

enter image description here

Don't bother with 'fixing' Resources.cs. When you save the .resx file it will be automatically regenerated with the correct types. The generated return type will be actually UnmanagedMemoryStream but don't be confused about that.

  1. Usage:
public static class WpfImages
{
    public static ImageSource Error { get; } = BitmapFrame.Create(Resources.Error);
    // [...]
}
<Image Source="{x:Static local:WpfImages.Error}"/>
like image 89
György Kőszeg Avatar answered Sep 22 '22 02:09

György Kőszeg


you can't do that. that worked only in winforms

see this post for more info

Different way how add image to resources

use the method shown in this post

WPF image resources

instead

quote:

If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

To do this, create a BitmapSource as a resource somewhere:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

Then, in your code, use something like:

<Image Source="{StaticResource MyImageSource}" />

In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

like image 26
Nahum Avatar answered Sep 22 '22 02:09

Nahum