我应该创建每个绘制请求新的笔/刷子或让他们在整个应用程序生命周期? [英] Should I create new Pens/Brushes per Paint request or keep them throughout the application lifecycle?

查看:190
本文介绍了我应该创建每个绘制请求新的笔/刷子或让他们在整个应用程序生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有做了的大量的应用程序的图纸,让我们pretend这是一个维索一样的应用。它有有被吸引,事情可以连接,调整大小等多个子对象的对象现在当我打电话油漆在一个特定的子对象或对象,我做到以下几点:

I have an application that does a lot of drawing, let's pretend it's a Viso-like application. It has objects that have multiple sub-objects that are drawn, things can be connected, resized etc. Currently when I call paint on a particular sub-object or object, I do the following:

using(var pen = new Pen(this.ForeColor))
{
    // Paint for this object.
}

我读过相互矛盾的答案,这应该是不断吸取同一事物的应用程序来完成(也许只是调整大小,移动等)。我应该保存笔/刷子的对象,然后所有的应用程序时,被处置,或者是他们有效率,足以创建/设置为每个处置的油漆调用(记住,这是一个pretty的图形密集应用程序)。

I've read conflicting answers that this should be done for an application that is constantly drawing the same thing (maybe just resized, moved etc). Should I store the Pen/Brush with the object and then dispose them all when the application is disposed of, or are they efficient enough to be created/disposed for each paint call (remembering that this is a pretty graphics intense application).

修改:现在已经有两个答案有相互矛盾的答案,这就是我不知道,使开关。没有人有任何统计上的差异?

EDIT: There are already two answers that have conflicting answers and this is where I'm not sure to make the switch. Does anyone have any stats on the differences?

推荐答案

您当然可以使用为您提供与已创建的运行时对象的画笔和画刷类。

You could of course use the Pens and Brushes classes that provide you with objects that are already created by the runtime.

例如,如果你想要的标准色笔,你可以这样做:

For example, if you want one of the standard colour Pens, you can do this:

var pen = Pens.Red;

同样,你也可以用,如果​​你只想做同样的标准的固刷色:

Likewise you can do the same with Brushes, if you just want standard solid brush colours:

var brush = Brushes.Red

使用这些,你不必担心清洁起来,处理它或以其他方式。

Using these, you don't need to worry about cleaning them up, disposing it or otherwise.

如果您希望您用一个不同的alpha分量,或渐变画笔也许是自己创建的,比如不同颜色的,那么你还需要创建这些自己,适当地清除它们。

If you want different colours that you create yourself, for example with a different alpha component, or a gradient brush perhaps, then you still need to create these yourself and clean them up appropriately.

编辑:

要创建和处理10万笔新的阵列花了大约半秒我的古老的老XP的机器,运行在调试模式下测试应用程序。

To create and dispose of an array of 100,000 new pens took approximately half a second on my ancient old XP machine, running a test app in Debug mode.

这相当于每栏约5微秒。只有你能决定,如果这是足够快你。我会大胆地猜测,这一次可能是微不足道的大部分关于您的操作的其余部分。

That equates to approximately 5 microseconds per pen. Only you can decide if that is fast enough for you. I would hazard a guess that this time may be largely insignificant with regard to the rest of your operations.

这篇关于我应该创建每个绘制请求新的笔/刷子或让他们在整个应用程序生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆