0

I have a png image with size 30x30. Then i created an image inside a button using my 30x30 image:

<Button>
  <Image x:Name="Sample" Source="sample.png" Stretch="None" SnapsToDevicePixels="True" 
         Width="30" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button>

But still the image is displayed big and cropped in the Image control. Why does this happen even they have the same size?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dyy
  • 51
  • 7

1 Answers1

1

The image in sample.png might have different DPI than 96, which WPF uses as the size of its device independent units.

Just don't set Stretch="None" on the Image element in order to get it scaled correctly. The default value of Stretch is Uniform.

<Image Source="sample.png" Width="30" Height="30"/>

You can check the difference between the native size and the (unstretched) rendered size of an image if you load it into a BitmapImage and compare its Width and Height with its PixelWidth and PixelHeight.

Clemens
  • 123,504
  • 12
  • 155
  • 268