【发布时间】:2012-03-05 03:53:25
【问题描述】:
我试图了解为什么我的图像不活泼,因此我构建了一个示例来测试 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);
最后,将每个图像的 Source 设置为位图:已按比例缩小到估计屏幕尺寸的灰度图像。位图生成如下:
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();
所以,我束手无策。图像出现明显延迟,似乎超出了我的控制。我能做什么?
【问题讨论】:
标签: wpf performance bitmap