Search code examples
wpfimagebitmaptreeviewtreeviewitem

Image size in TreeView depends on image color bit depth


I am experiencing a very strange thing: a TreeView contains items (Images), declared this way in XAML:

<ControlTemplate x:Key="ImageTemplate">
    <Image VerticalAlignment="Top" 
           Visibility="{Binding IsImageVisible,Converter={StaticResource BoolToVisibility}}"
           RenderOptions.BitmapScalingMode="NearestNeighbor"
           SnapsToDevicePixels="True"
           Stretch="None">
        <Image.Source>
            <BitmapImage UriSource="c:\\imageBMP4.bmp" />
            <!-- <BitmapImage UriSource="c:\\imageBMP8.bmp" /> -->
            <!-- <BitmapImage UriSource="c:\\imageBMP24.bmp" /> -->
            <!-- <BitmapImage UriSource="c:\\imageBMP32.bmp" /> -->
            <!-- <BitmapImage UriSource="c:\\imagePNG8.png" /> -->
            <!-- <BitmapImage UriSource="c:\\imagePNG24.png" /> -->
            <!-- <BitmapImage UriSource="c:\\imagePNG32.png" /> -->
            <!-- <BitmapImage UriSource="c:\\imageJPG24.jpg" /> -->
        </Image.Source>
    </Image>
</ControlTemplate>

All image are 640*480, filled with blue-white horizontal 1 pixel lines.

What I experience, if I use 4 or 8 bit bitmaps (bmp), they are rendered fine, pixel to pixel on the screen, size 640*480 in the tree. (however, scrolling the tree to the end causes rendering problems if more images are in the tree, see: TreeView/ScrollView rendering bug with bitmaps? ) Rendered image (8 bit bmp) in the tree (half sized):

enter image description here

However if I change to 24 or 32 bit image (bmp/png/jpg), they are strecthed to 853*640 (1.33 * original size), thus displayed strecthed/blurred. (however, this case, scrolling the tree to the end works fine, no rendering issues) Rendered image (24 bit bmp) in the tree (half sized):

enter image description here

What is this behavior, and why? Simple changing the bit depth of the source image why changes the rendered image dimensions?


Solution

  • Ok, I got the problem: WPF default DPI is 96, while my bitmaps were 72. Except the 8 and 4 bits, those were unspecified (this case the default 96 was considered). I converted bitmaps to 96dpi, and no resizing anymore. For more details, see the comment under the question.