Guidelines for Using Brushes and Pens

How expensive is it to create gdi brushes and pens? Should I create them on an add needed basis and wrap them in a using so they are disposed quickly, or should I create a static class similar to System.Drawing.Brushes class?

Asked on April 7, 2016 in .NET,   C#.
Add Comment
1 Answer(s)

IMO, they’re efficient enough that you should usually not create long-lived instances that are used over several method calls, but inefficient enough that you should create each one only once within a particular method, instead of creating a new one each time you need to draw something.

In other words, don’t store a custom brush in your class that you use to paint your text on every OnPaint call, but also don’t create a new brush for every line of text you draw within that OnPaint call, either.

Answered on April 7, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.