画笔和钢笔使用指南

发布于 2024-07-11 13:08:19 字数 106 浏览 4 评论 0原文

制作 GDI 画笔和钢笔有多贵? 我应该在添加所需的基础上创建它们并将它们包装在 using 中以便快速处理它们,还是应该创建一个类似于 System.Drawing.Brushes 类的静态类?

How expensive is it to create gdi brushes and pens? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-07-18 13:08:19

IMO,它们足够高效,您通常不应该创建在多个方法调用中使用的长期实例,但又不够高效,您应该在特定方法中只创建每个实例一次,而不是每次创建一个新实例。需要画一些东西。

换句话说,不要在您的类中存储用于在每次 OnPaint 调用时绘制文本的自定义画笔,但也不要为您绘制的每一行文本创建一个新画笔OnPaint 调用也是如此。

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.

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.

旧梦荧光笔 2024-07-18 13:08:19

当尝试使用静态画笔在负载下的 Web 应用程序中自定义绘制图像时,我遇到了异常。

我不知道技术原因,但我认为画笔和笔不是线程安全的,因此我会根据需要以线程安全的方式创建它们,并在该范围内处理它们,可能使用 using。

I've encountered exceptions when trying to custom draw images in a web application under load with a static brush.

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.

灼疼热情 2024-07-18 13:08:19

对于我们的可配置 GUI,我们认为在绘制控件时,我们会尝试将它们放入“使用”(在需要时重新创建),并认为如果需要,我们会进行优化。

还没有被要求。

这是 WinCE 上的 WinForm,这通常意味着它擅长显示在桌面上可能稍慢的东西(例如,在 WinCE 上反射感觉要昂贵得多),所以除非你在桌面上进行一些疯狂的图像处理,需要很多性能,否则我认为您应该可以在需要时创建它们。

IIRC 使用画笔。 GDI 中的集合可能是一个选项,因为这可以确保它们在最后被缓存和处置,尽管我不确定那里的线程安全性。

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.

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.

IIRC using the Brushes. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文