如何在WPF上显示图像更加“活泼”? [英] How can I make displaying images on WPF more “snappy”?

查看:97
本文介绍了如何在WPF上显示图像更加“活泼”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写WPF图像查看器,显示图像网格。由于性能低下,我感到困惑:即使是一个11 x 11的网格,也会让VM在长时间内没有响应,缓慢而缓慢。即使在功能强大的主机上,性能也非流行。

I’m writing a WPF image viewer, displaying a grid of images. I’m baffled because of the sluggish performance: Displaying even an 11 x 11 grid makes a VM unresponsive, slow and sluggish for long durations of times. Even on the powerful host the performance in non-snappy.

该程序基于SO WPF:在网格中排列集合项:ItemsControl绑定到Items,一个ObservableCollection。每个Item包含一个文件系统绝对URI。 ItemsControl的DataTemplate包含一个Image元素,其Source绑定到URI。

The program is based on the design in SO WPF: arranging collection items in a grid: An ItemsControl is bound to Items, an ObservableCollection. Each Item contains has a file-system absolute URI. The ItemsControl‘s DataTemplate contains an Image element whose Source is bound to the URI.

似乎问题不在于磁盘(SSD),内存(8GB VM,24GB主机)或CPU(i750)。此外,大部分工作都是由WPF完成的,所以我甚至不能在我的代码中找到问题:我的代码只是将URI(即图像的路径,而不是图像)加载到集合中并快速返回。然后有一个等待,WPF显示图像。

It seems the problem can’t be the disk (SSD), the memory (8GB VM, 24GB host), or CPU (i750). Moreover, most of the work is done by WPF, so it’s not even as if I could locate a problem in my code: My code simply loads URIs (i.e. the paths to the images, not the images) to the collection and quickly returns. Then there is a wait, and WPF displays the images.

我唯一能想到的问题是图像处理 - WPF缩小比例。但即使在拥有足够好的5850 ATI Radeon HD卡的主机上,性能也不是很快。

The only problem I could think about is image processing – down scaling by WPF. But even on the host, which has a "good-enough" 5850 ATI Radeon HD card, the performance is not snappy.

所以,我的问题是:如何在WPF上显示图像更干劲?

编辑:图像为1920x1080 22位HD JPEG捕获来自HD m2ts视频。我尝试将它们(使用FFmpeg)预缩放到'ega'640x350。 性能提升,但FFmpeg的缩小图像看起来比WPF差得多。

Edit: The images are 1920x1080 22 bit HD JPEGs captured from HD m2ts video. I tried pre-scaling them (using FFmpeg) to 'ega' 640x350. There was a performance improvement but FFmpeg's scaled-down images looks much worse than WPF's.

编辑:感谢David Osborne的代码现在以x64运行。仍然呆滞。

Edit: Thanks to David Osborne the code now runs as x64. Still sluggish.

编辑MatějZábský所谓的scalling图像是什么改善了这种情况:降低分辨率。为了未来读者的利益:

Edit What really improved the situation is what Matěj Zábský called scalling the images: reducing the resolution. For the benefit of future readers:

            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;


推荐答案

我一直处于类似的位置(我必须显示实时缩略图的大图像。

I have been in similar position (I had to display thumbnail versions of large images in real-time).

如果您使用数据绑定来显示图像,您可以尝试两件事:

If you are using data binding to display the images, you can try two things:


  • 进行绑定 OneWay (这对我帮助最大)。

  • 尝试制作绑定 async

  • Make the binding OneWay (this helped me the most).
  • Try making the binding async.

唯一的另一件事是预先缩放图像 - 最好是单独的线程。可能你甚至可以预先计量可能在将来使用的图像,而处理器处于空闲状态 - 例如,如果您正在开发类似地图的应用程序,则可以在用户最有可能移动的区域中预分割图像。

The only other thing is to prescale the images - preferably separate thread. Possibly you could even prescale images which could be potentially used in the future while the processor is idle - for example if you are developing a map-like application, you could prescale images in areas to which the user is most likely to move.

这篇关于如何在WPF上显示图像更加“活泼”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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