2016-08-03 109 views
1

有一個WPF(.NET 3.5)TreeView,其中包含位圖。 沒有什麼特別的:TreeView/ScrollView渲染與位圖錯誤?

<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:

<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> 

位圖作爲的BitmapSource生成imageConverter(來自天然對照)。

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()); 

它顯示一切正常,但是當我們滾動樹時,有時位圖會變形。大部分接近底部。有時我們需要調整樹的大小,但遲早會發生。

ItemX是文本,它旁邊的表是位圖。 enter image description here

之後,如果我錯了,如果我用滾動條位置大拇指向上滾動樹,只需要1個像素上/下,它就會變得清晰又好。 但是,如果我用了/滾動條上的向下箭頭滾動,位圖仍然無效幾點擊(然後走開)

如果我在XAML MultiBinding/imageConverter更換的BitmapSource代 - ><BitmapImage UriSource="c:\\imageBMP_test.bmp",則不會遇到此問題。 (我要照顧的imageBMP_test.bmp爲24位& 96DPI,否則也不好)

如果我改變的BitmapSource代從文件(96DPI 24位或32位BMP)閱讀:

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()); 

那麼一些滾動後,我得到這個: enter image description here

任何幫助,思想,將不勝感激。

我試過的:
檢查,生成的'BitmapSource'是96 DPI。
將所有生成的'BitmapSource'保存到文件中 - 一切正常,但它們在屏幕上變形。
我出於想法,似乎是一個WPF錯誤?或者也許是視頻驅動(的NVIDIA Quadro /膝上型/ WIN7 64位)

所以在XAML指定從文件加載SHORT

TreeView的顯示位圖:(這裏非常小的毛刺也有時,但這些都是可接受的)
TreeView顯示來自BitmapSource生成的位圖:FAILS有時(在滾動期間)

+0

只是爲了好玩嘗試使用'ViewBox'並參​​見。 – AnjumSKhan

+0

@AnjumSKhan你可以提供更多的細節,我應該在哪裏使用它? – Zotyi

+0

順便說一句,我擴展了原有的問題,更多的信息/示例/截圖 – Zotyi

回答

0

我找到了一個解決方案。 不知道爲什麼它的工作,但似乎解決了這個問題:

如果在imageConverter(在本地控制)我返回BitmapImage ^,而不是BitmapSource ^,沒關係。
很奇怪,但我只是從BitmapSource創建BitmapImage,返回並沒有問題。
WPF是可怕的...