Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it better approach to use a new Brush in Paint event i.e
C#
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
C#
private SolidBrush _b= null;

    private SolidBrush b
    {
        get
        {
            if (_b== null)
                return _b= new SolidBrush(Color.FromArgb(129, 242, 121));
            else
                return _b;
        }
    }
Posted

It all depends on whether you want to use the same brush in more than one method. If you need to reuse it then declare it as a class level variable and dispose it in OnDispose method, else just create it as in your first example.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900