darthmaule2
Topic Author
Posts: 94
Joined: 23 Oct 2014, 19:54

Trying to bind to a BitmapImage created with a Uri pointing to an embedded resource

14 May 2019, 19:56

I have a folder called "Images" which contains an embedded resource "MyImage.png".

I can display that image without any issues if I do it like this
    <Image Source="Images/MyImage.png"></Image>
But, I can't load the image using a Uri from code-behind... I can see that FileTextureProvider::OpenStream(string path) is returning null.
    <Image Source="{Binding MyImage}"></Image>

        public MainWindow()
        {
            DataContext = this;

            BitmapImage test = new BitmapImage(new Uri("Images/MyImage.png", UriKind.Relative));
            test.Freeze();
            MyImage = test;
        }

        BitmapSource _myImage;
        BitmapSource MyImage
        {
            get
            {
                return _myImage;
            }
            set
            {
                _myImage = value;
                OnPropertyChanged("MyImage");
            }
        }


Is there a way to do this?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Trying to bind to a BitmapImage created with a Uri pointing to an embedded resource

17 May 2019, 00:32

Is that Images folder under another folder in the C# project? Because embedded resources are stored using the whole project path.

The xaml parser is able to build the full path because it uses xaml folder as base uri. So for example, if you have Xamls/MainMenu/MainMenu.xaml and Xamls/MainMenu/Images/MyImage.png, you can write <Image Source="Images/MyImage.png"/> because it composes the path by prefixing it with Xamls/MainMenu.

From code-behind you should provide the full path as we do in our samples: https://github.com/Noesis/Tutorials/blo ... del.cs#L21
BitmapImage test = new BitmapImage(new Uri("pack://application:,,,/MyProject;component/Xamls/MainMenu/Images/MyImage.png"));

Who is online

Users browsing this forum: No registered users and 1 guest