Question

After looking for the answer in both Microsoft documentation and forums, I am at a loss. I am loading a png image as a background for an inkCanvas (WPF) which works fine, however, it always resizes the image to fit in the canvas, despite the image size..

Here was my last attempt with no success:

            BitmapImage ii = new BitmapImage(new Uri(path));
            Image img = new Image();

            img.Stretch = Stretch.None;
            img.Source = ii;
            InkCanvas1.Background = new ImageBrush(ii);

Here is what it looks like using Stretch.None and Stretch.Fill

enter image description here

Here is what I am trying to achieve:

enter image description here

Can this be done?

Was it helpful?

Solution

The problem is that you're trying to set properties on an Image object that you're not using while ignoring the same settings on the ImageBrush that you are using. The Image in this case is just being thrown away and the ImageBrush just happens to be using the same source image. Set the Stretch property on the ImageBrush instead:

    BitmapImage ii = new BitmapImage(new Uri(path));

    ImageBrush imageBrush = new ImageBrush(ii);
    imageBrush.Stretch = Stretch.Uniform;
    InkCanvas1.Background = imageBrush;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top