【问题标题】:Image object rendering is extremly slow in WPF DataTemplateWPF DataTemplate 中的图像对象渲染非常慢
【发布时间】:2012-10-11 00:19:24
【问题描述】:

我正在创建一个带有缩略图的简单文件浏览器。我正在使用带有自定义 DataTemplate 的 ListBox 在 ObservableCollection 中显示对象。

<DataTemplate>
   <StackPanel Margin="5">
      <Image Source="{Binding Path=ThumbnailPath}"/>
      <Label Background="White" Content="{Binding Path=FileName}"/>
   </StackPanel>
</DataTemplate>

(我的自定义类 File 的)对象只有这两个字符串属性:ThumbnailPath 和 FileName。当用户选择一个文件夹时,BackgroundWorker 会获取文件列表并创建 File 类的实例。使用 BW 的 ReportProgress 将这些实例分派到 UI 线程(以 10 个为一组)。在事件处理程序中,它们被添加到绑定到我的 ListBox 的 ObservableCollection 中。

问题是我的收藏中至少要添加 20-30 个文件;在 ListBox 更新之前,UI 冻结了将近三秒钟。甚至不要问当一个文件夹包含数百个文件时会发生什么。一切都在后台做好了准备,所以我猜问题出现在 WPF 开始初始化和渲染空的 Image 元素时。当我从 DataTemplate 中注释掉 Image 时,需要一眨眼的时间来更新集合及其视图。

对此有什么可以做的吗?我知道可以在后台线程中创建整个 View 对象(一个新的 StackPanel,添加子新标签和新图像,设置值),但是 DataBinding 和模板的全部意义应该是避免这样做的需要......所以如何在 ListBox 的 DataTemplate 中填充图像而不失去响应能力?

PS:实际的缩略图是由 FFmpeg 生成并保存到文件中的,但是这个过程只有在显示所有项目(带有空白图像对象)后才开始,因此在本题的上下文中它们无关紧要。

【问题讨论】:

标签: wpf data-binding backgroundworker datatemplate observablecollection


【解决方案1】:

试试这个:

将ThumbnailPath改为BitmapImage的类型;

设置属性使用时

BitmapImage bi1 = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
bi1.BeginInit();
bi1.UriSource = new Uri(@"C:\filepath.jpg");
 To save significant application memory, set the DecodePixelWidth or   
// DecodePixelHeight of the BitmapImage value of the image source to the desired  
// height or width of the rendered image. If you don't do this, the application will  
// cache the image as though it were rendered as its normal size rather then just  
// the size that is displayed. 
// Note: In order to preserve aspect ratio, set DecodePixelWidth 
// or DecodePixelHeight but not both.
bi1.DecodePixelWidth = 200;
bi1.EndInit();
bi1.Freeze();

//if you do not Freeze, your app will leak memory.

【讨论】:

  • Jpeg 一切都很好,但是 tiff 图像在性能方面不能很好地处理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多