Wpf Canvas Background image does not display selected image from local path
XAML Code
<Canvas x:Name="LayoutRoot" Margin="485,24,0,0" HorizontalAlignment="Left" Width="341" Height="506" VerticalAlignment="Top">
<Canvas.Background>
<ImageBrush ImageSource="{Binding BGImage}"/>
</Canvas.Background>
</Canvas>
MVVM code
private String _BGImage = @"C:/Users/sam/Desktop/photo-5.jpg";
public String BGImage
{
get
{
return this._BGImage;
}
set
{
this._BGImage = value;
NotifyPropertyChanged("BGImage");
}
}
Why this image not display on canvas background
Learn how to set an image as background on your Fabric.js canvas easily. In some cases you may want to define an abstract image background in your canvas of Fabric, so the background won't be never mixed with the shapes and elements that you add to the canvas. You can easily define a background image using the setBackgroundImage method. A.
Change your desktop background image. Select Start > Settings > Personalization > Background, and then select a picture, solid color, or create a slideshow of pictures. Want more desktop backgrounds and colors?
In some cases you may want to define an abstract image background in your canvas of Fabric, so the background won't be never mixed with the shapes and elements that you add to the canvas. You can easily define a background image using the setBackgroundImage method.
To pick a new color, click the palette icon, and then the Add a new color tile. From the editor side panel, click Background. If you don’t see it, click ••• More first. Type “gradient” on the search bar, and hit Enter or Return on your keyboard. Click on any of the results to apply it to your design.
or you can try using a converter
<UserControl.Resources>
<local:StringToImageConverter x:Key="StringToImageConverter" />
</UserControl.Resources>
...
<Canvas x:Name="LayoutRoot" Margin="485,24,0,0" HorizontalAlignment="Left" Width="341" Height="506" VerticalAlignment="Top">
<Canvas.Background>
<ImageBrush ImageSource="{Binding Path=BGImage, Converter={StaticResource StringToImageConverter}}"/>
</Canvas.Background>
</Canvas>
and this is the converter
public class StringToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.GetType() != typeof(string))
{
throw new InvalidOperationException("The value must be a string");
}
return new BitmapImage(new Uri((string)value));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
of course you would still need to check if the string is a valid URI
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