Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image within a WPF application displays smaller than when viewed in external viewer

Tags:

image

wpf

sizing

When I display a JPEG in my WPF application (using the following code), it is shown significantly smaller than if I open the JPEG in the Windows Picture Viewer at actual size.

I've drilled into the properties of my ImageSource at runtime and my image has:

  • a DPI of 219
  • a Height of 238.02739726027397
  • a Width of 312.54794520547944
  • a PixelHeight of 543
  • and a PixelWidth of 713

When I use a screen ruler to measure the WPF display of the image, I get approx. 313x240 pixels (which if I could positiont the ruler perfectly would probably be equal to the Width and Height that the ImageSource is reporting.).

My gut tells me this has something to do with WPF's use of device independent units (instead of pixels) but I can't make sense of it, and I still need to know how to display the image at the 'actual' size of 543x713 in my application.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    <StackPanel>
        <Image Source="Image15.jpg" Stretch="None" />
    </StackPanel>
</Window>
like image 567
joshuapoehls Avatar asked Mar 13 '09 20:03

joshuapoehls


1 Answers

Thanks Mark! I did some Googling based on your info and found this article that provided a solution to get the result I wanted. This is starting to make sense now...

Edit: Linkrot. Pasting the critical text from the article here for reference....

<Image Source=”{Binding …}”
       Stretch=”Uniform”
       Width=”{Binding Source.PixelWidth,RelativeSource={RelativeSource Self}}”
       Height=”{Binding Source.PixelHeight,RelativeSource={RelativeSource Self}}” />

Here we’ve set Stretch to Uniform and bound the Width and Height to the PixelWidth and >PixelHeight of the Source, effectively ignoring DPI. The image however will not be pixel >perfect, even when using SnapToDevicePixels (which simply snaps the borders, not pixels >within the image). However, WPF in 3.5 SP1 will support a NearestNeighbor >BitmapScalingMode, which should correct this.

like image 79
joshuapoehls Avatar answered Oct 28 '22 02:10

joshuapoehls