Avi*_*Avi 8 wpf performance bitmap
我试图理解为什么我的图像不够活泼,所以我构建了一个样本来测试WPF性能.我使用计时器来计算我的"显示图像"事件处理程序执行了多长时间,并使用秒表来测量图像在屏幕上显示的时间.底线:当显示100,1600,2500和3600张图像时,WPF 在我的代码完成后花了2,9,12和16秒在屏幕上显示图像.所以我感到无助:似乎我无法改进我的代码以使图像显得更快 - 我需要用WPF做点什么!
所以我的问题是:我需要做些什么来使图像显示更快?
测试设置很简单:
该窗口包含一个网格.单击"测试"按钮后,将添加行和列定义.然后将图像添加到网格的每个单元格,如下所示:
var image = new Image();
image.BeginInit();
image.Name = ImageNameFromCell(theRow, theColumn);
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Center;
image.VerticalAlignment = VerticalAlignment.Center;
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.LowQuality);
image.EndInit();
theGrid.Children.Add(image);
Run Code Online (Sandbox Code Playgroud)
最后,将每个图像的源设置为位图:灰度图像已经缩小到估计的屏幕尺寸.位图生成如下:
var smallerBitmapImage = new BitmapImage();
smallerBitmapImage.BeginInit();
smallerBitmapImage.DecodePixelWidth = (int)(theImageWidth);
smallerBitmapImage.UriSource = theUri;
smallerBitmapImage.CacheOption = BitmapCacheOption.None;
smallerBitmapImage.EndInit();
//BitmapFrame bitmapFrame = BitmapFrame.Create(this.FullPath);
var convertedBitmap = new FormatConvertedBitmap();
convertedBitmap.BeginInit();
convertedBitmap.Source = smallerBitmapImage;
convertedBitmap.DestinationFormat = PixelFormats.Gray16;
convertedBitmap.EndInit();
convertedBitmap.Freeze();
Run Code Online (Sandbox Code Playgroud)
所以,我的智慧结束了.图像显示有明显的延迟,似乎不受我的控制.我能做什么?
造成差异的原因似乎是将图像的缓存选项设置为 OnLoad
lesserBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
这将工作移至我的事件处理程序,因此现在我可以使用预取在后台执行此操作。