在我的 OnPaint 覆盖中绘制后是否需要恢复图形状态(对于 .NET 控件)
考虑以下 .NET 控件的重写 OnPaint 方法:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.RotateTransform(180);
// lots of drawing code
}
当我完成时没有恢复 e.Graphics 对象的状态是否有问题?
在 Java 中,这通常是通过复制传递的 Graphics 对象来完成的,因此传递的 Graphics 对象不会改变,也不需要恢复它的状态。 类似的Java问题
我可以通过使用 Save() / Restore() 方法在 .NET 中实现此目的。所以我的问题是:
- .NET 中恢复图形状态是否有必要/最佳实践?
- Save() / Restore() 方法的成本有多高?
Consider the following overriden OnPaint method for a .NET Control:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.RotateTransform(180);
// lots of drawing code
}
Is it a problem that I do not restore the state of the e.Graphics object when I am finished?
In Java this is often done by making a copy of the passed Graphics object, thus the passed Graphics object is not altered and there is no need to restore it's state.
Similar Java Question
I could achieve this in .NET by using the Save() / Restore() methods. So my questions are:
- is it neccessary/best practice in .NET to restore the graphics state ?
- how expensive are the Save() / Restore() methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,无论谁要为控件实现 Paint 事件,都会感到有点惊讶。如果所有东西总是轮换是有意义的,那么它可能是有效的,但这由你决定。
在我的笔记本电脑上,Graphics.Save + Restore 大约需要 4 微秒。考虑到绘图的典型费用,无需担心。
Well, whomever is going to implement the Paint event for the control is going to have a bit of a surprise. It could be valid if it makes sense that everything is always rotated, but that is for you to decide.
Graphics.Save + Restore takes about 4 microseconds on my laptop. Nothing to worry about given the typical expense of drawing.