GDI 中的经验法则+

发布于 2024-08-03 01:22:47 字数 264 浏览 6 评论 0原文

我一直在 .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 技术交流群。

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

发布评论

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

评论(7

她说她爱他 2024-08-10 01:22:47

尽可能晚地创建对象(不要过早优化/缓存)并尽早释放它们(如果 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).

痴骨ら 2024-08-10 01:22:47

不要避免使用非托管调用,如果正确完成,它可以大大加快速度。

Dont avoid using unmanaged calls, it can speed things up a lot when done correctly.

油饼 2024-08-10 01:22:47

在逻辑/屏幕坐标之间转换时要小心。如果在错误的时间完成,您可能会失去精度并得到一些令人讨厌的绘图瑕疵。

上次它咬我时,我试图计算逻辑坐标中的一个新点;但这些已经接近精度极限,因此新点并不完全是人们所希望的。尽早转型解决了这个问题。

过早转换到屏幕坐标可能会遇到类似的问题,尽管允许您传递浮点坐标的 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.

爱已欠费 2024-08-10 01:22:47

不要画得过多。

一般来说,绘图操作比您要执行的其他计算更昂贵(特别是在 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.

(り薆情海 2024-08-10 01:22:47

GDI 陷阱曾多次让我恼火。

  • 克隆不会克隆()基础数据克隆(矩形,像素格式)。因此,如果您释放了clone(),原始对象将变得不可用。如果您想要两个单独的位图,请使用 new Bitmap()。
  • 如果您从 File 加载图像,则该文件将被锁定,直到位图被处理(甚至无法读取)。
  • 使用DrawImage时不要忘记设置SmoothingMode、InterpolationMode和PixelOffsetMode,否则你会对图像的低质量感到惊讶。

GDI Gotchas that have burned me a few times.

  • Clone doesn't clone() the underlying data clone(Rectangle, PixelFormat) does. So if you dispose of a clone(), the original object becomes unusable. Use new Bitmap() if you want two seperate bitmaps.
  • If you load an image FromFile that file is locked until the bitmap is disposed of (can't even be read).
  • When using DrawImage don't forget to set SmoothingMode, InterpolationMode and PixelOffsetMode or you will be surprised by the low quality of the image.
半寸时光 2024-08-10 01:22:47

严格来说,这不是 GDI+ 问题,但请记住这一点,这样可以节省几个小时的调试时间:

我在 .NET 中使用 GDI+ 经常犯的一个错误是在 TransformMatrix 方法Graphics 对象的 code> 属性。示例(在 C++/CLI 中):

g->Transform->Translate(100.0f, 250.0f);    // WRONG. Will not have any effect.

Transform 属性的 getter 仅返回矩阵的副本。因此,在此副本上调用的任何方法都不会影响图形变换的值。要操作图形变换,请调用包装器方法之一(MultiplyTransformTranslateTransformScaleTransform 等),如下所示。

g->TranslateTransform(100.0f, 250.0f);

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 the Transform property of the Graphics object. Example (in C++/CLI):

g->Transform->Translate(100.0f, 250.0f);    // WRONG. Will not have any effect.

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.

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