Topic Author
Posts: 95
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));
            MyImage = test;

        BitmapSource _myImage;
        BitmapSource MyImage
                return _myImage;
                _myImage = value;

Is there a way to do this?

User avatar
Site Admin
Posts: 2323
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: Bing [Bot], P376EM and 4 guests