views:

42

answers:

1

With the following code, I can't get the background image to display at its original size (it's always a fair bit larger). I tried changing the Stretch property, but "None" seems to be the most appropriate value.

<Canvas Width="500" Height="500">
    <Canvas.Background>
        <ImageBrush 
            ImageSource="{Binding ElementName=w_window, Path=BackgroundImagePath}" 
            Stretch="None"
            AlignmentX="Left"
            AlignmentY="Top"
            />
    </Canvas.Background>
</Canvas>

In the image below, you can see my app on the left and photo viewer (set to original size) on the right. Comparison of size as rendered by my application (left) and a photo viewer (right)

Any advice? (BTW, setting ClipToBounds="True" doesn't work.)

It seems that my problem isn't restricted to when the image is a background. The following code, in which an Image is the only element, has the same sizing problem. It's not just my machine having this problem (i.e. the app has this problem on other machines).

<Window x:Class="DiagramDesigner.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="1000">
    <Image 
        Source="C:\Users\patk\Downloads\Bottom_wnums.jpg"
        Stretch="None"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        />
</Window>
+1  A: 

Hi, I encountered this issue with images before. Nevertheless I was using PNG files instead of JPG at that time, but I assume the problem might be related. It may be an issue with the image you are using (as the code you posted looks fine). Images should be stored in 96 DPI (which is the standard). Other values may cause the image to scale. If you really can't change/fix the image to fulfil this requirement, you might want to check this post from Scott Hanselman to understand the problem a little more in order to find the right answer.

Julian Dominguez
I had tried it with several other formats (PNG, BMP, JPG), but I hadn't thought to check the image resolution - it was at 72dpi. Thanks for figuring it out!
Pat