Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a lot of rectangles in GDI+

I have an application drawing a lot of small rectangles (about 1 million) using GDI+ in C# and the performance is... not so good. I understand that constructing a lot of Rectangle objects for passing them to Graphis.FillRectangle takes some time. I also understand that I cannot avoid this if the Rectangles change from from frame to frame, because Rectangle is designed to be immutable. My question therefore is: is there any possibility to improve the performance of this drawing call except if all Rectangles must be assumed to be visible?

Thanks in advance, Christoph

like image 703
Christoph Avatar asked Apr 20 '12 12:04

Christoph


1 Answers

I suggest you use the array variant of the call instead:

FillRectangles(Brush brush, Rectangle[] rects);

If that does not work for you because you need different brushes or the like, you should group the primitives by state (brush) and draw the arrays each in a single go. A quick test shows at least three times the performance, i.e. 300K Rectangles drawn individually feel about as choppy as 1M Rectangles drawn in one go.

like image 84
Keiichi Avatar answered Oct 14 '22 21:10

Keiichi