How to set Image Source in C# to XAML Static Resource programmatically?

70,586

Solution 1

After much Googling, whilst writing this question, I figured out how to do it:

TypeIcon.Source = (ImageSource) Resources["Project"];

Solution 2

It is not for static resources but perhaps will be useful anyway... :)

i.e. how to set background for Grid dynamically

var myBrush = new ImageBrush();
            var image = new Image
                            {
                                Source = new BitmapImage(
                                    new Uri(
                                        "pack://application:,,,/YourAppName;component/Images/Boo.png"))
                            };
myBrush.ImageSource = image.Source;
MainGrid.Background = myBrush;

i.e. how to set icon of the app dynamically

var idleIco = new Image
            {
                Source = new BitmapImage(
                    new Uri(
                        "pack://application:,,,/YourAppName;component/Images/idle.ico"))
            };
SomeObjectYouAreUsingToSet.IconSource =idleIco.Source;

Solution 3

You can use the ImageSourceConverter class to get what you want, for example:

img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Assets/check.png");
Share:
70,586
Danny Beckett
Author by

Danny Beckett

I'm a PHP/JS developer & C#/XAML programmer from Liverpool . I lived in Amsterdam for a couple of years, and Oslo for a little while too. OS windows-server centos GUI c# .net wpf visual-studio Web php html5 css3 javascript jquery SQL mysql mssql t-sql oracle pl-sql sybase-asa sqlanywhere HTTPd apache iis

Updated on July 09, 2022

Comments

  • Danny Beckett
    Danny Beckett almost 2 years

    I have this ResourceDictionary in Main.xaml:

    <Window.Resources>
        <ResourceDictionary>
            <BitmapImage x:Key="Customer" UriSource="Icons/customer.png"/>
            <BitmapImage x:Key="Project" UriSource="Icons/project.png"/>
            <BitmapImage x:Key="Task" UriSource="Icons/task.png"/>
        </ResourceDictionary>
    </Window.Resources>
    

    I initially set the image using:

    <Image Name="TypeIcon" HorizontalAlignment="Left" VerticalAlignment="Center"
        Source="{StaticResource Customer}" Height="16" Width="16"/>
    

    I'm trying to change TypeIcon's Source from Customer to Project in a C# method.

    I've tried using:

    TypeIcon.Source = "{StaticResource Project}";
    

    But I get this error:

    Cannot implicitly convert type string to System.Windows.Media.ImageSource

    I've tried defining the image using new ImageSource(), but this doesn't work either.

    How can I change the image's Source programmatically in C#?

  • JP Hellemons
    JP Hellemons almost 10 years
    The Media namespace is unavailable for universal apps System.Windows.Media.ImageSourceConverter
  • Starwave
    Starwave almost 5 years
    What is what here? For "ImageSource" you also need to include "using System.Windows.Media;". But "Resources" is still not available in current context...