Data binding performance issues

2019-05-25 20:03发布

问题:

I am writing a map control that can display a bench of geometries. For better performance, I draw all my geometries using DrawingVisuals which I then write into a RenderTargetBitmap as shown in the code below:

public class Map{
   public ImageSource MapDrawingImage{get;set;}

   private void RenderMap(){
    MapDrawingImage= new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Pbgra32);
    foreach (Layer layer in map.Layers) {
        System.Windows.Media.DrawingVisual layerDrawing = Render(layer, map);
        MapDrawingImage.Render(layerDrawing);
    }
   }
}

In order to display the map, the main window has an Image control which Source is set to Map.MapDrawingImage image source. To automatically update the image, I use the following data binding:

    RenderOptions.SetBitmapScalingMode(mapImage, BitmapScalingMode.LowQuality);
    // Map image binding
    Binding mapBinding = new Binding();
    mapBinding.Source = map;
    mapBinding.Path = new PropertyPath("MapDrawingImage");
    mapImage.SetBinding(System.Windows.Controls.Image.SourceProperty, mapBinding); 

This works very well when the map is static. However, in a dynamic mode where the map is updated at a rate of 5 to 10 times a second, the data binding seems to fall a bit short behind and the application slows down. I have searched for long days and I found out that:

  1. RenderTargetBitmap does not use hardware acceleration thus causing some delays when rendering the map.
  2. Data binding might also cause some delays refreshing the map image

Is there any better way to improve the map performance (RenderTargetBitmap replacement, data binding improvement) ?

Thanks in advance,

回答1:

Databinding is updated in a seperate thread. So this will always be with a delay. Also it works with a queue, so when the databinding can't keep up the queue will grow bigger and bigger. The solution would be that you use an image which is static and change the image itself instead of replacing it with another image.