Best practice for personalized painting in Winforms?

Usually, when I override the OnPaint method, I create pens and brushes in it, etc., and then delete them.

I also read somewhere that instead of recreating these pens and brushes, etc., create them once as static members, and then delete them once when the form is closed, etc.

Is this the best practice?

Is there a better way to do this?

I can assume that since OnPaint is called 1000s (?) Times, this would create more work for the GC compared to creating them only once.

+5
source share
5 answers

, . , , ( ), ThreadStatic ( ), ( Dispose ); GDI +, GDI + . .

(, , ), , ( - ).

, , , Brushes Pens, .Net SystemBrushes SystemPens .

+6

, BufferedGraphics on_paint .

, , BufferedGraphics, - (.. ).

+2

, , . , / OnPaint.

static, , , . , SLaks, , , , .

+1

, . -, , ad-hoc, .

, Dispose() , . , , , . GDI GDI.

, - , , , . , , , , .

Finally, at least for this message, remember to use double buffering, either automatic in the .net control system, or in the style of roll-your-own-back-bitmap.

Good luck GDI-ing :)

+1
source

The best practice is to use system pens and brushes, as they are optimized for minimal resource consumption.

0
source

All Articles