简体   繁体   English

使用画笔和笔的指南

[英]Guidelines for Using Brushes and Pens

How expensive is it to create gdi brushes and pens? 创建gdi笔刷和笔有多贵? Should I create them on an add needed basis and wrap them in a using so they are disposed quickly, or should I create a static class similar to System.Drawing.Brushes class? 我应该在需要的基础上创建它们并将它们包装在一个使用中,以便快速处理它们,或者我应该创建一个类似于System.Drawing.Brushes类的静态类?

IMO, they're efficient enough that you should usually not create long-lived instances that are used over several method calls, but inefficient enough that you should create each one only once within a particular method, instead of creating a new one each time you need to draw something. IMO,它们足够高效,你通常不应该创建在多个方法调用中使用的长期存在的实例,但效率低,你应该在特定的方法中只创建一个,而不是每次都创建一个新的需要画一些东西。

In other words, don't store a custom brush in your class that you use to paint your text on every OnPaint call, but also don't create a new brush for every line of text you draw within that OnPaint call, either. 换句话说,不要在您的类中存储用于在每次OnPaint调用上绘制文本的自定义画笔,也不要为在OnPaint调用中绘制的每一行文本创建新画笔。

I've encountered exceptions when trying to custom draw images in a web application under load with a static brush. 我尝试使用静态画笔在加载的Web应用程序中自定义绘制图像时遇到异常。

I don't know the technical reasons, but I think Brushes and Pens are not thread safe, so I would create them as needed in a thread-safe manner and dispose of them within that scope, probably with a using . 我不知道技术原因,但我认为Brushes和Pens不是线程安全的,所以我会根据需要以线程安全的方式创建它们并将它们放在该范围内,可能是使用它

For our configurable GUI we figured we'd try just putting them in a "using" (re-create whenever required) when drawing our controls and figured we'd optimise if required. 对于我们的可配置GUI,我们认为在绘制控件时我们会尝试将它们放入“使用”(在需要时重新创建),并且如果需要我们会进行优化。

It hasn't been required. 它没有被要求。

This is WinForm on WinCE which generally means its good at showing up things that might be slightly slow on the desktop (reflection feels much more expensive on WinCE for example) so unless you are doing some insane image manipulation on desktop that requires mucho perf then I think you should be fine creating them as and when required. 这是WinCE上的WinForm,这通常意味着它擅长于在桌面上显示可能稍慢的东西(例如,在WinCE上反射感觉要贵得多),所以除非你在桌面上做一些疯狂的图像操作,需要很多性能然后我认为你应该在需要的时候创建它们。

IIRC using the Brushes. IIRC使用刷子。 collection in the GDI might be an option as this ensures they get cached and disposed at the end although i'm not sure about thread safety there. GDI中的集合可能是一个选项,因为这可以确保它们在最后进行缓存和处理,尽管我不确定那里的线程安全性。

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

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