Caching Pens and Brushes inside the ColorTable.

May 16, 2008 at 7:11 PM
After experimenting with the initial release of this code, I noticed that drawing the different ribbon items creates and disposes of a significant number of pens and brushes.  Perhaps the RibbonProfessionalRendereColorTable class could be enhanced to cache frequently-used pens or brushes:

private static Hashtable _pens = new Hashtable( );

GetCachedPen( Color clr )
    if (_pens.Contains( clr ))
        pen = _pens[ clr ] as Pen
        pen = new Pen
( clr );
        _pens[ clr ] = pen;
    return pen;

Then the drawing code could be simplified to something like:

e.Graphics.DrawPath( ColorTable.GetCachedPen(PanelLightBorder), light );


    Humble Programmer



Jul 28, 2008 at 10:30 AM
Aaa, this helps a bit! Thanks!

I am currently going through all the suggestions and making the changes to see if some of the issues can be sorted!

I will post the code as a patch if I'm done.
Feb 5, 2009 at 3:07 PM
I would advice against doing this!
Pens and brushes are SYSTEM-WIDE resources, so it's better to keep them for a relatively short time.
Especially when you're rendering brushes with gradients that, for instance, use the size of a control, 
you'd have to cache a lot of different brushes and update them when a control changes size.
... Which is exactly when you're drawing the most and exactly when the caching does not help you!
May 16, 2013 at 12:26 PM
Hi, a new version of Ribbon is released. Please visit the new site for latest fixes, releases, information and downloads.
New site: