H
H
HitGirl2019-06-14 16:00:18
.NET
HitGirl, 2019-06-14 16:00:18

How to optimize line drawing in WPF?

Hello!
Can you please tell me how to optimize line drawing in WPF? If you add points to a polyline in the OnMouseMove event handler, Wpf starts to slow down when there are several thousand points in the polyline collection.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cyber_roach, 2019-06-16
@cyber_roach

From several thousand, anything will slow down.
The direction was already indicated to you, I will add:
In order not to eat all the RAM and percent,
read about BitmapCache
https://docs.microsoft.com/en-us/dotnet/api/system...
and RenderTargetBitmap
https ://docs.microsoft.com/ru-ru/dotnet/api/system...
in UWP caching at a higher level is implemented, it is simpler there, but in WPF you can also do it well.
The algorithm in short:
2 layers - the top one - where you poke with the mouse
and the bottom one - the cache
Added a line (on the top layer) - render to a picture (top + bottom) - draw this picture on the bottom layer (for example, through ImageBrush), delete the line on the top.
Those. as soon as the user has finished the figure / action - cache.
If you need an interactive on an already drawn one (for example, to highlight the nodes of points), it’s more difficult, but also possible (to remember the coordinates of the nodes and, when the mouse is there, create a Xaml object.)
With interactive in such algorithms, as a rule, there is a lot of mathematics.
Those. you always have 1 picture in RAM + some objects - polylines.
By the way, in addition to drawing, through caching, you can make quite productive animations, productive zooming, productive scrolling ...
There is another option: implement DirectX drawing in C ++ and place a control. More native and more productive.
P/S
property IsHitTestVisible
https://docs.microsoft.com/ru-ru/dotnet/api/system...
allows the object not to react to the mouse (i.e. disables interactive) this is useful for background objects, greatly boosts performance.
(because by default WPF defines all interactive events on all objects, and there can be an infinite number of them, and not all of them need to have a reaction to pressing / OnMouseMove)

R
rPman, 2019-06-14
@rPman

It slows down probably because you redraw all objects on on paint (at least when adding a new one?) Draw lines to the bitmap once and draw it on on paint.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question