GDI 中的经验法则+
我一直在 .NET 中编写一些 GDI+ 代码,并且一直在艰难地吸取教训。简单的事情比如:
- 在屏幕上看起来不错的东西在纸上看起来可能不太好,反之亦然
- 缓存太多对象可能会导致 OutOfMemoryException
- 浮点数不精确
......等等。我确信有经验的人可以对此添加更多内容。
使用 GDI+ 或任何一般图形库时需要遵循哪些良好规则?
每篇文章提供一个有用的提示就很好了。谢谢。
I have been working on some GDI+ code in .NET and have been learning my lessons the hard way. Simple things like:
- What looks good on screen may not look nice on paper and vice versa
- Caching too many objects can result in an OutOfMemoryException
- Floats aren't exact
...and so on. I'm sure there is a lot more that experienced folk can add to this.
What are some good rules to follow when using GDI+ or any graphics library in general?
One useful tip per post will be nice. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尽可能晚地创建对象(不要过早优化/缓存)并尽早释放它们(如果 IDisposable 则调用 Dispose 或包装在 using 语句中)。
Create objects as late as possible (Don't prematurely optimize/cache) and release them as early as possible (calling Dispose or wrapping in using statement if IDisposable).
不要避免使用非托管调用,如果正确完成,它可以大大加快速度。
Dont avoid using unmanaged calls, it can speed things up a lot when done correctly.
在逻辑/屏幕坐标之间转换时要小心。如果在错误的时间完成,您可能会失去精度并得到一些令人讨厌的绘图瑕疵。
上次它咬我时,我试图计算逻辑坐标中的一个新点;但这些已经接近精度极限,因此新点并不完全是人们所希望的。尽早转型解决了这个问题。
过早转换到屏幕坐标可能会遇到类似的问题,尽管允许您传递浮点坐标的 API(GDI+ 就是这样做的)往往对此更加稳健。
Careful when you transform between logical/screen coordinates. If done at the wrong time you can run out of precision and get some nasty drawing artifacts in return.
The last time this bit me, I was trying to calculate a new point in logical coordinates; those were already near the limit of precision though, so the new point was not quite what one would hope. Transforming sooner fixed it.
It is possible to get a similar problem by transforming to screen coordinates too soon, although an API which allows you to pass it floating point coordinates (which GDI+ does) tends to be much more robust against that.
不要画得过多。
一般来说,绘图操作比您要执行的其他计算更昂贵(特别是在 GDI+ 中,这是一个很好的 API,但不是有史以来最快的绘图库)。在您自己的代码中多花一点时间来避免不必要的绘图操作(例如多次绘制同一事物)通常是值得的。
Don't draw more than you have to.
In general, drawing operations are more expensive than other calculations you'll be performing (especially in GDI+, which is a nice API but not the fastest drawing library ever). Spending a bit more time in your own code to avoid unnecessary drawing operations (eg. drawing the same thing more than once) is often well worth it.
GDI 陷阱曾多次让我恼火。
GDI Gotchas that have burned me a few times.
严格来说,这不是 GDI+ 问题,但请记住这一点,这样可以节省几个小时的调试时间:
我在 .NET 中使用 GDI+ 经常犯的一个错误是在
TransformMatrix
方法Graphics
对象的 code> 属性。示例(在 C++/CLI 中):Transform
属性的 getter 仅返回矩阵的副本。因此,在此副本上调用的任何方法都不会影响图形变换的值。要操作图形变换,请调用包装器方法之一(MultiplyTransform
、TranslateTransform
、ScaleTransform
等),如下所示。Not strictly a GDI+ issue but remember this to save yourself a few hours of debugging:
One mistake I've made too often using GDI+ in .NET is to call
Matrix
methods on theTransform
property of theGraphics
object. Example (in C++/CLI):The getter of the
Transform
property only returns a copy of the matrix. So any methods called on this copy will not affect the value of the graphics transform. To manipulate the graphics transform, call one of the wrapper methods (MultiplyTransform
,TranslateTransform
,ScaleTransform
, etc.) as shown below.请小心如何使用区域: http://steveperks.com/post/Fun -With-GDI2b-Bugs.aspx
Be careful how you use regions: http://steveperks.com/post/Fun-With-GDI2b-Bugs.aspx