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>
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:
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>
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.
public static class WpfImages
{
public static ImageSource Error { get; } = BitmapFrame.Create(Resources.Error);
// [...]
}
<Image Source="{x:Static local:WpfImages.Error}"/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With