带有位图的TreeView/ScrollView渲染错误? [英] TreeView/ScrollView rendering bug with bitmaps?

查看:93
本文介绍了带有位图的TreeView/ScrollView渲染错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个WPF(.NET 3.5)TreeView,其中包含位图. 没什么特别的:

There is a WPF (.NET 3.5) TreeView, which contains bitmaps. Nothing special:

<ControlTemplate x:Key="ctrlOutputTree" TargetType="{x:Type vo:OutputTreeView}">
    <TreeView x:Name="OutputTree" ItemContainerStyle="{DynamicResource TreeViewItemStyle}"
              KeyboardNavigation.TabNavigation="Cycle" SnapsToDevicePixels="True"  MouseDown="OutputTree_MouseDown"
              Loaded="OutputTree_Loaded" RenderOptions.BitmapScalingMode="NearestNeighbor">

        <TreeView.Resources>
            <Style TargetType="TreeViewItem">
                <EventSetter Event="RequestBringIntoView" Handler="OutputTree_RequestBringIntoView"/>
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
            </Style>
        </TreeView.Resources>

        <TreeViewItem  ItemsSource="{Binding Converter={StaticResource featureDiffsSourceCreator},Mode=OneWay}"
                       IsExpanded="True" Margin="0,4,0,0" 
                       KeyDown="AttrDiffTreeviewItem_KeyDown">
        </TreeViewItem>
    </TreeView>
</ControlTemplate> 

TreeViewItems:

TreeViewItems:

<ControlTemplate x:Key="ImageTemplate">
    <Image VerticalAlignment="Top" Margin="3,1,0,0" 
           RenderOptions.BitmapScalingMode="NearestNeighbor"
           RenderOptions.EdgeMode="Aliased"
           Stretch="None">
        <Image.Source>
            <!-- <BitmapImage UriSource="c:\\imageBMP_test.bmp" />-->

            <MultiBinding Converter="{StaticResource imageConverter}">
                <Binding Path=....
            </MultiBinding>
        </Image.Source>
    </v:Image>
</ControlTemplate>

位图在imageConverter中作为BitmapSource生成(来自本机控件).

The bitmaps are generated as BitmapSource in imageConverter (from native control).

System::Drawing::Bitmap^ b = System::Drawing::Image::FromHbitmap((IntPtr)aBmp);
IntPtr hb = b->GetHbitmap();
System::Windows::Media::Imaging::BitmapSource^ bs = 
    System::Windows::Interop::Imaging::CreateBitmapSourceFromHBitmap(hb, 
                             System::IntPtr::Zero,
                             Int32Rect(0,0,nWidth,nHeight),
                             BitmapSizeOptions::FromEmptyOptions());

它显示一切正常,但是当我们滚动树时,有时位图会变形.大多接近底部.有时我们需要调整树的大小以使其发生,但它迟早会发生.

It displays all fine, but when we scroll the tree, sometimes bitmaps becomes distorted. Mostly close to the bottom. Sometimes we need to resize the tree to happen, but it happens sooner or later.

ItemX是文本,它旁边的表是位图.

ItemX is text, and the table next to it is the bitmap.

此后,如果出现错误,如果我使用滚动条-位置-拇指滚动树,只需向上/向下移动1个像素,它就会变得又清晰又好. 但是,如果我使用滚动条上的上/下箭头滚动,则单击几下位图仍然无效(然后消失)

After this, when it's wrong, if I scroll the tree with the scrollbar-position-thumb, just 1 pixel up/down, it becomes sharp and good again. But if I scroll with up/down arrows on the scrollbar, bitmaps remain invalid for a few click (then goes away)

如果我在XAML MultiBinding/imageConverter-> <BitmapImage UriSource="c:\\imageBMP_test.bmp"中替换了BitmapSource生成,则不会遇到此问题. (我必须注意imageBMP_test.bmp为24位和96DPI,否则也不好)

If I replace the BitmapSource generation in XAML MultiBinding/imageConverter --> <BitmapImage UriSource="c:\\imageBMP_test.bmp" , then don't experience this issue. (I have to take care the imageBMP_test.bmp to be 24 bits & 96DPI, otherwise also not good)

如果我将BitmapSource的生成更改为从文件中读取(96 dpi的24或32位bmp):

If I change the BitmapSource generation to read from file (24 or 32bit bmp with 96dpi):

System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap("c:\\imageBMP24_96dpi_small.bmp");
IntPtr hb = b->GetHbitmap();
System::Windows::Media::Imaging::BitmapSource^ bs = 
    System::Windows::Interop::Imaging::CreateBitmapSourceFromHBitmap(hb, 
                             System::IntPtr::Zero,
                             Int32Rect(0,0,nWidth,nHeight),
                             BitmapSizeOptions::FromEmptyOptions());

然后经过一些滚动,我得到了:

Then after some scrolling, I got this:

任何帮助,我们将不胜感激.

Any help, idea would be appreciated.

我尝试过的事情:
已检查,生成的"BitmapSource"为96 DPI.
将所有生成的"BitmapSource"保存到文件中-都很好,但它们在屏幕上变形了.
我没有主意,似乎是WPF错误?还是视频驱动程序? (nvidia quadro/laptop/win7 64bit)

What I tried:
Checked, generated 'BitmapSource' is 96 DPI.
Saved into file all generated 'BitmapSource' - all were fine, while they were distorted on the screen.
I am out of ideas, seems it's a WPF bug? Or maybe video driver? (nvidia quadro/laptop/win7 64bit)

短暂进入

TreeView显示从XAML中指定的文件加载的位图:确定(有时在这里也有很小的故障,但是可以接受)
TreeView显示从生成的BitmapSource中生成的位图:有时失败(在滚动过程中)

TreeView displays bitmap loaded from file specified in XAML: OK (very small glitches here also sometimes, but those are acceptable)
TreeView displays bitmap from BitmapSource generated: FAILS sometimes (during scroll)

推荐答案

我找到了解决方案. 不知道它为什么起作用,但是似乎可以解决问题:

I found a solution. Have no clue why it works, but seems to solve the problem:

如果在imageConverter中(在本机控件中),我返回BitmapImage ^而不是BitmapSource ^,就可以了.
很奇怪,但是我只是从BitmapSource创建BitmapImage,将其返回,没有问题.
WPF太可怕了...

if in the imageConverter (in a native control) I return BitmapImage^ instead of BitmapSource^, it's ok.
Very strange, but I just create BitmapImage from the BitmapSource, return that, and no problems.
WPF is horrible...

这篇关于带有位图的TreeView/ScrollView渲染错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆