在winforms应用程序中缓存GDI +对象:是否值得,如何正确执行?

时间:2012-02-14 23:03:42

标签: c# .net winforms gdi+ system.drawing

对于我的一些winforms应用程序,我需要创建一大堆GDI +对象(画笔,笔,字体等)并一遍又一遍地使用它们。我创建了一个贫民窟缓存单例来完成我需要的东西,但代码味道压倒性......

public sealed class GraphicsPalette
{
    public static readonly GraphicsPalette Instance = new GraphicsPalette();

    static GraphicsPalette()
    {
    }

    private Dictionary<Color, Brush> solidBrushes;

    //multithreading
    private object brushLock;

    private GraphicsPalette()
    {
        solidBrushes = new Dictionary<Color, Brush>();

        brushLock = new object();
    }

    public Brush GetSolidBrush(Color color, int alpha)
    {
        return GetSolidBrush(Color.FromArgb(alpha, color));
    }

    public Brush GetSolidBrush(Color color)
    {
        if (!solidBrushes.ContainsKey(color))
        {
            lock (brushLock)
            {
                if (!solidBrushes.ContainsKey(color))
                {
                    Brush brush = new SolidBrush(color);
                    solidBrushes.Add(color, brush);
                    return brush;
                }
            }
        }
        return solidBrushes[color];
    }
}
  1. 我是否有更好的方法来重用这些GDI +对象,而不是每次调用OnPaint()等时再次实例化它们?
  2. 一旦程序终止,GDI +对象是否会导致非托管内存泄漏,或者是否会调用每个Brush对象的终结器,从而释放任何非托管资源?
  3. 如果这是重复,我道歉,但我没有发现任何类似的问题。

2 个答案:

答案 0 :(得分:5)

没有内存泄漏但最好在你有意义时发布GDI +对象。操作系统中的数量有限,因此您可能会在您和其他应用程序中导致渲染问题。另一件需要提及的是GDI +对象(字体等)无法同时被2个线程使用(可能会抛出一些难以重现的异常)。您可能对实际GDI +对象创建时间与可能的独占锁定延迟的一些测量感兴趣。 “过早优化是所有邪恶的根源”©Donald Knuth

实际上,对我来说,做一些GDI +对象缓存是有用的:每个绘画周期。客户端代码可能如下所示:

class Visual 
{
    public void Draw() 
    {
        using (new GraphicsPalette()) {
            DrawHeader();
            DrawFooter();
        }
    }

    private void DrawHeader() {
        var brush = GraphicsPalette.GetSolidBrush(Color.Green);
        ...   
    }

    public void DrawFooter() { 
        using (new GraphicsPalette()) { // ensures palette existence; does nothing if there is a palette on the stack
            var brush = GraphicsPalette.GetSolidBrush(Color.Green); // returns the same brush as in DrawHeader
            ...
        }
    }
}

因此我们需要GraphicsPalette忽略嵌套构造并返回给定线程的相同画笔。建议的解决方案:

public class GraphicsPalette : IDisposable 
{
    [ThreadStatic]
    private static GraphicsPalette _current = null;
    private readonly Dictionary<Color, SolidBrush> _solidBrushes = new Dictionary<Color, SolidBrush>();

    public GraphicsPalette() 
    {
        if (_current == null)
            _current = this;
    }

    public void Dispose() 
    {
        if (_current == this)
            _current = null;

        foreach (var solidBrush in _solidBrushes.Values)
            solidBrush.Dispose();            
    }

    public static SolidBrush GetSolidBrush(Color color) 
    {
        if (!_current._solidBrushes.ContainsKey(color))
            _current._solidBrushes[color] = new SolidBrush(color);

        return _current._solidBrushes[color];
    }
}

答案 1 :(得分:0)

根据我对VG.net的经验,我不相信缓存GDI +对象通常是值得的,除了像Bitmaps这样的大件事。当然,它很容易衡量。