I'm working on a project were I need to draw N
ellipses every X
milliseconds, where 0 <= N <= 10^5
and 5 <= X <= 500
.
I started using WPF, and its relative controls like Canvas
for the viewport and Ellipse
controls for the ellipses. I store the ellipses in a list and:
- Every time the number of ellipses increases I add a new ellipse to my list.
- Every time the number of ellipses stored in
myViewport.Children
is less thanmyListOfEllipses.Count
I add these ellipses to my viewport, otherwise I remove the ellipses in excess from the viewport without remove them from my list. - For each ellipse I compute the new position and I use the
Canvas.SetLeft
andCanvas.SetTop
methods.
The performance is quite good, but there is a slowdown when X
is set to the minimum and N
is greater than 1000.
I thought about using DrawingVisual
s, but I suppose that drawing and displaying a bitmat is also faster. So, I decide to replace my viewport with an Image
control that has as Source
a WriteableBitmap
and to use WriteableBitmapEx
library.
- Every time I clear the viewport using
myBitmap.Clear(Colors.White)
. - Every time I draw new ellipses using
myBitmap.FillEllipse(...)
.
Well, the problem is the latter way is much slower than the former. How is it possible? Where am I wrong? How can I improve the performance of my bitmap?