Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set background image to a Canvas

Tags:

wpf

xaml

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

like image 523
Sam Alex Avatar asked Apr 15 '15 12:04

Sam Alex


People also ask

How to set an image as background on a fabric canvas?

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.

How do I change the background image on my Desktop?

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?

How do I define an abstract image background in fabric?

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.

How do I change the background color of my website?

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.


1 Answers

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

like image 53
memory of a dream Avatar answered Sep 20 '22 10:09

memory of a dream