【问题标题】:Image size in TreeView depends on image color bit depthTreeView 中的图像大小取决于图像颜色位深度
【发布时间】:2016-08-03 15:05:57
【问题描述】:

我遇到了一件非常奇怪的事情:TreeView 包含项目(图像),在 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>

所有图片为640*480,用蓝白色横1像素线填充。

我的经验是,如果我使用 4 位或 8 位位图 (bmp),它们会在屏幕上以像素到像素的方式呈现,树中的大小为 640*480。 (但是,如果树中有更多图像,将树滚动到最后会导致渲染问题,请参阅:TreeView/ScrollView rendering bug with bitmaps?) 树中的渲染图像(8 位 bmp)(一半大小):

但是,如果我更改为 24 位或 32 位图像 (bmp/png/jpg),它们会被拉伸到 853*640(1.33 * 原始大小),因此显示为拉伸/模糊。 (但是,在这种情况下,将树滚动到最后工作正常,没有渲染问题) 树中的渲染图像(24 位 bmp)(一半大小):

这是什么行为,为什么?简单改变源图像的位深度为什么会改变渲染图像的尺寸?

【问题讨论】:

  • 我认为这可能是原因:Actually, I've seen that regularly with PNG images. It seems that the default resolution for PNG is 72dpi, while the default screen resolution in WPF is 96dpi. WPF tries to take this into account by rendering png bitmaps scaled to 133% of their pixel size, which is technically correct but normally not what the user wants. You can either use a gif or jpg instead, or you can combine the image with a LayoutTransform scaling it to 0.75 of its size, or just stick with setting the size of the image control explicitly.

标签: wpf image bitmap treeview treeviewitem


【解决方案1】:

好的,我遇到了问题:WPF 默认 DPI 是 96,而我的位图是 72。除了 8 位和 4 位之外,这些都未指定(本例中考虑了默认 96)。 我将位图转换为 96dpi,不再调整大小。 有关更多详细信息,请参阅问题下方的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多