Should I create new Pens/Brushes per Paint request or keep them throughout the application lifecycle?

后端 未结 5 2185
北海茫月
北海茫月 2021-02-19 02:33

I have an application that does a lot of drawing, let\'s pretend it\'s a Viso-like application. It has objects that have multiple sub-objects that are drawn, things can

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 03:28

    You could of course use the Pens and Brushes classes that provide you with objects that are already created by the runtime.

    For example, if you want one of the standard colour Pens, you can do this:

    var pen = Pens.Red;
    

    Likewise you can do the same with Brushes, if you just want standard solid brush colours:

    var brush = Brushes.Red
    

    Using these, you don't need to worry about cleaning them up, disposing it or otherwise.

    If you want different colours that you create yourself, for example with a different alpha component, or a gradient brush perhaps, then you still need to create these yourself and clean them up appropriately.

    EDIT:

    To create and dispose of an array of 100,000 new pens took approximately half a second on my ancient old XP machine, running a test app in Debug mode.

    That equates to approximately 5 microseconds per pen. Only you can decide if that is fast enough for you. I would hazard a guess that this time may be largely insignificant with regard to the rest of your operations.

提交回复
热议问题