Is it better approach to use a new Brush in Paint event i.e
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (SolidBrush b = new SolidBrush(Color.FromArgb(129, 242, 121))) {
for (int i = 0; i < 12; i++) {
e.Graphics.FillPath(b, path[i]);
}
}
base.OnPaint(e);
}
or define once at top and dispose in Dispose Method i.e
SolidBrush _brush;
protected SolidBrush Brush {
get {
if (_brush == null)
_brush = new SolidBrush(Color.FromArgb(129, 242, 121));
return _brush;
}
}
Creating and destroying drawing objects like pens and brushes is very cheap, takes about a microsecond. A very small fraction of the cost of the code that actually does the drawing, typically measured in milliseconds. You should therefore avoid storing them, that just takes up precious space in the operating system GDI object heap, a resource that needs to be shared by all running processes. The only drawing object that's expensive to create is a font. However, Winforms solves this by caching fonts internally.
Make it consistent, always apply the using statement to drawing objects you create.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With