Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guidelines for Using Brushes and Pens

Tags:

c#

.net

gdi

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?

like image 412
Bob Avatar asked Jan 07 '09 15:01

Bob


People also ask

How do you use brush pens?

To use a brush pen, hold it close to the nib to give you more control over your strokes. Try to keep your wrist and fingers still and move your arm to direct the pen. Use upstrokes and apply light pressure to create thin lines. For thick lines, use downstrokes and apply heavy pressure.

Why do we use brush pens?

They're also great if you're into crafts; watercolor brush pens allow you to create unique styles like dainty florals and hand lettering that can be used to make cards, invitations and art prints. On top of that, they can be used for adult coloring books, too.


2 Answers

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.

like image 51
P Daddy Avatar answered Sep 21 '22 19:09

P Daddy


I've encountered exceptions when trying to custom draw images in a web application under load with a static brush.

I don't know the technical reasons, but I think Brushes and Pens are not thread safe, so I would create them as needed in a thread-safe manner and dispose of them within that scope, probably with a using.

like image 42
Greg Avatar answered Sep 22 '22 19:09

Greg