简体   繁体   English

什么是在用户控件中配置画笔的更好方法

[英]What is better approach to dispose Brush in User Control

Is it better approach to use a new Brush in Paint event ie 是否更好的方法在Paint事件中使用新的Brush,即

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 ie 或者在顶部定义一次并在Dispose方法中处理即

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. 因此,您应该避免存储它们,这只占用操作系统GDI对象堆中的宝贵空间,这是一个需要由所有正在运行的进程共享的资源。 The only drawing object that's expensive to create is a font. 创建的唯一绘图对象是字体。 However, Winforms solves this by caching fonts internally. 但是,Winforms通过在内部缓存字体来解决这个问题。

Make it consistent, always apply the using statement to drawing objects you create. 使其一致,始终将using语句应用于您创建的绘图对象。

Use predefined Brushes if you can (and do not dispose them). 如果可以,请使用预定义的画笔 (并且不要丢弃它们)。 If you can't I suggest do not create your brushes on every paint but cache them: 如果你不能建议不要在每个油漆上创建刷子,而是缓存它们:

IDictionary<Color, Brush> SolidBrushes; //... cache
Brush GetSolidBrush(Color color) {
    if(color.IsSystemColor) 
        return GetSystemBrush(color);
    Brush result = null;
    if(!SolidBrushes.TryGetValue(color, out result)) {
        result = new SolidBrush(color);
        SolidBrushes.Add(color, result);
    }
    return result;
}
Brush GetSystemBrush(Color color) {
    return SystemBrushes.FromSystemColor(color);
}

ADDITION: The best answer on this question may be "depends on task". 附加:关于这个问题的最佳答案可能是“取决于任务”。 The brush creation is expensive, because of the brushes themselves (it is managed wrapper on unmanaged GDI+ object), and also because of the garbage collection with all those brushes on every Paint event. 画笔创建是昂贵的,因为画笔本身(它是非托管GDI +对象上的托管包装),也是因为每个Paint事件上的所有画笔都进行了垃圾收集。 So if you use multiple brushes it is better to cache them (of course the cached brushes should be disposed on disposing of owner control or on skin changing). 因此,如果你使用多个刷子,最好缓存它们(当然,缓存的刷子应该在处置所有者控制或皮肤更换时处理)。 But if you use only one brush (the first case) thr cache is not needed - only use the brush in using block 但是如果你只使用一个画笔(第一种情况)不需要thr缓存 - 只使用使用块中的画笔

从性能的角度来看,我更喜欢创建一个画笔并将其配置在Dispose方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM