在 Windows 窗体中呈现文本的最快 API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/71374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest API for rendering text in Windows Forms?
提问by Dave Moore
We need to optimize the text rendering for a C# Windows Formsapplication displaying a large number of small strings in an irregular grid. At any time there can be well over 5000 cells visible that update 4 times per second. The font family and size is consistent across the cells, though the color may vary from cell to cell, as will bold/italic/plain.
我们需要优化在不规则网格中显示大量小字符串的 C# Windows 窗体应用程序的文本呈现。任何时候都可能有超过 5000 个可见的单元格每秒更新 4 次。单元格之间的字体系列和大小是一致的,尽管颜色可能因单元格而异,粗体/斜体/普通也是如此。
I've seen conflicting information on the web about TextRenderer.DrawText
vs. Graphics.DrawString
being the fastest/best, which reduces to a GDIvs. GDI+comparison at the Win32level.
我在网上看到了关于TextRenderer.DrawText
与Graphics.DrawString
最快/最好的相互矛盾的信息,这简化为Win32级别的GDI与GDI+比较。
I've also seen radically different results on Windows XP vs. Windows Vista, but my main target is Windows XP. Articles promising great advances under WinFXand DirectX 10aren't helpful here :-)
我还在 Windows XP 和 Windows Vista 上看到了完全不同的结果,但我的主要目标是 Windows XP。承诺在WinFX和DirectX 10下取得巨大进步的文章在这里没有帮助:-)
What's the best approach here? I'm not afraid of introducing a small C++/CLI layer and optimizing device context handling to squeeze out more performance, but I'd like some definitive advice about which direction to take.
这里最好的方法是什么?我不害怕引入一个小的 C++/CLI 层并优化设备上下文处理以挤出更多的性能,但我想要一些关于采取哪个方向的明确建议。
EDIT: Thanks for the initial responses. I'll be trying a combination of background bitmap rendering and sticking with the GDI equivalent calls.
编辑:感谢您的初步答复。我将尝试将背景位图渲染和坚持使用 GDI 等效调用相结合。
采纳答案by Mike Dimmick
A Microsoft developer has posted a GDI vs. GDI+ Text Rendering Performancearticle on his blog which answers the raw speed question: on his system, GDI DrawText was about 6 times faster than GDI+ DrawString.
一位 Microsoft 开发人员在他的博客上发布了一篇GDI 与 GDI+ 文本渲染性能文章,该文章回答了原始速度问题:在他的系统上,GDI DrawText 比 GDI+ DrawString 快约 6 倍。
If you need to be a real speed demon, TextOut is faster than DrawText, but you'll have to take care of clipping and word-wrapping yourself. ExtTextOut supports clipping.
如果您需要成为真正的速度恶魔,TextOut 比 DrawText 快,但您必须自己处理剪辑和自动换行。ExtTextOut 支持剪辑。
GDI rendering (TextRenderer) will be more consistent with other parts of Windows using GDI; GDI+ tries to be device-independent and so some spacing and emboldening are inconsistent. See the SQL Server 2005 Surface Area Configuration tool for an example of inconsistent rendering.
GDI 渲染(TextRenderer)将与使用 GDI 的 Windows 其他部分更加一致;GDI+ 尝试独立于设备,因此一些间距和底气是不一致的。有关不一致呈现的示例,请参阅 SQL Server 2005 表面区域配置工具。
回答by Mike Dimmick
5000+ text rendering is slow even with GDI, especially if you need scrolling. Create a separate rendering thread and notify the UI thread every 200 ms and bitblt the current results. It gives a smooth user experience.
即使使用 GDI,5000+ 文本渲染也很慢,尤其是在您需要滚动时。创建一个单独的渲染线程,每 200 ms 通知 UI 线程并 bitblt 当前结果。它提供了流畅的用户体验。
回答by Judah Gabriel Himango
Creating a C++/CLI interop class to do the drawing in native code will result in crazy-fast drawing. We've witnesses this and measured it.
创建 C++/CLI 互操作类以在本机代码中进行绘制将导致绘制速度非常快。我们见证了这一点并对其进行了测量。
If you're not up to doing that, we've found graphics.DrawString is just slightly faster than than TextRenderer.DrawText.
如果您不愿意这样做,我们会发现 graphics.DrawString 比 TextRenderer.DrawText 略快。
回答by fritz
On my Windows 7 64 Bit system TextOut is even a bit slower than DrawString! TextRenderer.DrawText is much slower than DrawString.
在我的 Windows 7 64 位系统上,TextOut 甚至比 DrawString 慢一点!TextRenderer.DrawText 比 DrawString 慢得多。
回答by Phil Wright
GDI is faster at drawing in general that GDI+. I worked on a project that had to draw thousands of lines and text strings and switching from GDI+ to GDI made a significant performance improvement. That was using Windows XP so I cannot comment on Vista. I would also recommend using double buffering for your drawing to also improve performance. Create a compatible off screen bitmap and reuse that each time you need to draw.
一般来说,GDI 在绘图方面比 GDI+ 更快。我参与了一个必须绘制数千行和文本字符串的项目,从 GDI+ 切换到 GDI 显着提高了性能。那是使用 Windows XP,所以我无法评论 Vista。我还建议为您的绘图使用双缓冲来提高性能。创建一个兼容的屏幕外位图并在每次需要绘制时重复使用。