- darthmaule2
- Posts: 98
- Joined:
Trying to bind to a BitmapImage created with a Uri pointing to an embedded resource
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
But, I can't load the image using a Uri from code-behind... I can see that FileTextureProvider::OpenStream(string path) is returning null.
Is there a way to do this?
I can display that image without any issues if I do it like this
Code: Select all
<Image Source="Images/MyImage.png"></Image>
Code: Select all
<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?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Trying to bind to a BitmapImage created with a Uri pointing to an embedded resource
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
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
Code: Select all
BitmapImage test = new BitmapImage(new Uri("pack://application:,,,/MyProject;component/Xamls/MainMenu/Images/MyImage.png"));
Who is online
Users browsing this forum: Semrush [Bot] and 13 guests