Rendering graphic objects in WPF

Currently I have an image on canvas that can be freely moved in my application, on which I have 6 layers of DrawingVisuals, but it looks pretty slow. I am using RenderTargetBitmap for rendering. Is there a faster way to display images on an image or on any other frame element that I can freely move around the canvas?

XAML:

<Canvas> <Image Height="505" HorizontalAlignment="Left" Margin="0,0,0,0" Name="_MapImage" Stretch="Fill" VerticalAlignment="Top" Width="700" MouseLeftButtonDown="_MapImage_MouseDown" MouseWheel="_MapImage_MouseWheel" MouseLeftButtonUp="_MapImage_MouseUp" MouseMove="_MapImage_MouseMove" /> </Canvas> 

the code:

 _renderRoutesBitmap = new RenderTargetBitmap((int)(_MapImage.Width), (int)(_MapImage.Height), 96, 96, PixelFormats.Default); for (int i = 6; i < 8; ++i) { if((layerCode / (int)Math.Pow(10,i) % 2) == 1) _renderRoutesBitmap.Render(_layers[i]); //takes too much time } _RouteImage.Source = _renderRoutesBitmap; 
+6
source share
2 answers

I had to do something a while ago when I had to write my own GIS application. I had to draw thousands and thousands of graphic images, my results were that RenderTargetBitmap not a good choice for operations with bitmaps, as it suffers from the use of graphics hardware acceleration. Silverlight has a more appropriate class; WriteableBitmap , which allows your application to write directly to the GPU buffer. The only problem is that it is only available for Silverlight. If you want to use the bitmap for your operations, you can use the WriteableBitmap equivalent for WPF, which WriteableBitmapEx is available here .

Since you have only 6 graphic visual images, I suggest you switch to using a higher level UI element, such as shapes, etc.

+2
source

Rendering visual objects using Render is not a good solution. If you have a canvas and want to visualize visual visibility, you do not need to convert visual images to a bitmap, also if you do the conversion, the image cannot be scaled by any sums without degrading the quality (vector graphics function). There is one more possibility: create your own canvas. I am currently using this method and I have no problem drawing thousands of shapes. This is a very simple example:

 public class DrawingCanvas : Panel { public List<Visual> visuals = new List<Visual>(); public void AddVisual(Visual visual) { if (visual == null) return; this.visuals.Add(visual); base.AddVisualChild(visual); base.AddLogicalChild(visual); } public void RemoveVisual(Visual visual) { if (visual == null) return; this.visuals.Remove(visual); base.RemoveVisualChild(visual); base.RemoveLogicalChild(visual); } } 

Use a DrawingCanvas instead of a Canvas class.

+2
source

Source: https://habr.com/ru/post/914409/


All Articles