WPF位图性能(WPF Bitmap performance)

编程入门 行业动态 更新时间:2024-10-26 06:31:01
WPF位图性能(WPF Bitmap performance)

我试图理解为什么我的图像不灵活,所以我构建了一个样本来测试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);

最后,每个图像的源被设置为位图:已经缩小到估计的屏幕尺寸的灰度图像。 位图生成如下:

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();

所以,我在我的智慧结束。 图像显着延迟,似乎超出了我的控制范围。 我能做什么?

I'm trying to understand why my images are not snappy, so I built a sample to test WPF performance. I used a timer to calculate how long my "display images" event handler executed, and used a stop watch to measure how long it took the images to appear on the screen. The bottom line: when displaying 100, 1600, 2500 and 3600 images, WPF took 2, 9, 12 and 16 seconds after my code had finished to display the images on the screen. So I feel helpless: It seems I can't improve my code to make the images appear faster - I need to do something with WPF!

So my question is: What do I need to do differently to make the images display faster?


The test setup is simple:

The window contains a Grid. After the "test" button is clicked, row and column definitions are added.Then an Image is added to each cell of the grid as follows:

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);

Finally, the Source of each image is set to a bitmap:a gray-scale image already scaled down to the estimated screen size. The bitmap is generated as follows:

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();

So, I'm at my wits end. The images appear with a noticeable delay, and it seems to be out of my control. What can I do?

更多推荐

本文发布于:2023-08-01 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   性能   WPF   Bitmap   performance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!