【发布时间】:2012-02-16 02:56:09
【问题描述】:
我正在编写一个 WPF 图像查看器,显示图像网格。由于性能缓慢,我感到困惑:即使显示 11 x 11 网格也会使 VM 长时间无响应、缓慢且缓慢。即使在强大的主机上也表现得非常出色。
该程序基于 SO WPF: arranging collection items in a grid 中的设计:一个 ItemsControl 绑定到 Items,一个 ObservableCollection。每个 Item 包含一个文件系统绝对 URI。 ItemsControl 的 DataTemplate 包含一个 Image 元素,其 Source 绑定到 URI。
似乎问题不在于磁盘 (SSD)、内存(8GB VM、24GB 主机)或 CPU (i750)。此外,大部分工作都是由 WPF 完成的,所以我什至无法在我的代码中找到问题:我的代码只是将 URI(即图像的路径,而不是图像)加载到集合中并快速返回。然后等待,WPF 显示图像。
我能想到的唯一问题是图像处理——通过 WPF 缩小比例。但即使在拥有“足够好”的 5850 ATI Radeon HD 卡的主机上,性能也不是很出色。
所以,我的问题是:如何让 WPF 上的图像显示更加“快捷”?
编辑:图像是从高清 m2ts 视频中捕获的 1920x1080 22 位高清 JPEG。我尝试将它们(使用 FFmpeg)预缩放为 'ega' 640x350。 有性能改进,但 FFmpeg 的按比例缩小的图像看起来比 WPF 的差很多。
编辑:感谢 David Osborne,代码现在以 x64 格式运行。还是很慢。
编辑 真正改善这种情况的是 Matěj Zábský 所说的缩放图像:降低分辨率。为了未来读者的利益:
fullPath = new Uri(path, UriKind.Absolute);
BitmapImage smallerBitmapImage = new BitmapImage();
smallerBitmapImage.BeginInit();
smallerBitmapImage.DecodePixelWidth = (int) (theWidthOfTheGrid / theNumberOfColumns);
smallerBitmapImage.UriSource = fullPath;
smallerBitmapImage.EndInit();
FormatConvertedBitmap formatConvertedBitmap = new FormatConvertedBitmap();
formatConvertedBitmap.BeginInit();
formatConvertedBitmap.Source = smallerBitmapImage;
formatConvertedBitmap.DestinationFormat = PixelFormats.Gray16;
formatConvertedBitmap.EndInit();
formatConvertedBitmap.Freeze();
this.ImageSource = formatConvertedBitmap;
【问题讨论】:
-
所以你有一个“已知”图像的静态网格?图片有多大?
-
未来读者面临这个问题 - 在这里查看我的答案:stackoverflow.com/questions/9265725/wpf-bitmap-performance
-
你在使用阴影吗?
标签: wpf performance image