使用GDI +在C#/.NET中更快地绘制文字? [英] Faster text drawing in C#/.NET with GDI+?

查看:171
本文介绍了使用GDI +在C#/.NET中更快地绘制文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个用于WinForms应用程序的自定义控件-基本上,它是一个十六进制编辑器控件,它显示偏移量,字节行,文本列和其他一些信息.
<我正在OnPaint中进行所有绘制,但是我开始注意到,当控件很大时(因此绘制了很多字节),渲染变得有些滞后.在极端情况下,绘制整个控件大约需要20到30毫秒.这并不可怕,但是它会使拖动选择感觉有些缓慢,而且我敢肯定,在低端计算机上,这种选择会更糟.

该控件完全基于文本-除了几个Graphics.DrawPath/FillPath调用,大部分GDI +调用是Graphics.DrawString(每个显示的字节一个,以允许自定义列间距).这些DrawString调用最终占用了绝大多数的渲染时间.

是否有更快的方法在GDI +中进行文本绘制?似乎可以尝试减少DrawString调用的次数,这是一个不错的选择,但是在保持对字符间距的控制的同时又如何做到呢?

也许还有另一种更简单的方法为此我只是不知道呈现文本?任何建议将不胜感激.

I''m currently writing a custom control for use with a WinForms application - basically, it''s a hex editor control, which displays offsets, rows of bytes, a text column, and some other information.

I''m doing all the drawing inside OnPaint, but I''ve started to notice that rendering becomes a little laggy when the control is large (and thus a lot of bytes are being drawn). In extreme cases it can take around 20 to 30 milliseconds to draw the entire control. This isn''t terrible, but it makes dragging selections feel very slightly sluggish, and I''m sure it''s much worse on lower-end machines.

The control is entirely text based - aside from a couple Graphics.DrawPath/FillPath calls, the bulk of the GDI+ calls are to Graphics.DrawString (one for each byte displayed, to allow for custom column spacing). These DrawString calls end up taking up the vast majority of the rendering time.

Is there any faster way to do text drawing in GDI+? It seems that it would be a good move to try to reduce the number of DrawString calls, but how can I do that while still maintaining control over the spacing of characters?

Perhaps there''s another, simpler way of rendering text for this purpose that I''m just unaware of? Any advice would be appreciated.

推荐答案

这是一门很难掌握的技术,但是在GDI +下可以非常快速地绘制文本.这里有太多细节要讨论,但是您可以在xacc.ide中查看我的"AdvancedTextBox"控件.源代码位于sourceforge上.

总结:
-标记数据
-通过将相似的标记分组来优化标记(存储该标记,如果行/文本"为无效",则将其无效")更改)
-绘制分组

我还建议您坚持使用等宽字体.然后只需要调用1即可获得一个字符的宽度.

It is a hard art to master, but text can be drawn very fast under GDI+. There is just too much detail to go into, but you can look at my ''AdvancedTextBox'' control in xacc.ide. The source code is on sourceforge.

To summarize:
- Tokenize data
- Optimize tokens by grouping similar ones (store this and ''invalidate'' if the ''line/text'' changes)
- Paint the groupings

I would also suggest sticking to monospace fonts. Then only 1 call is needed to get the width of a character.


这篇关于使用GDI +在C#/.NET中更快地绘制文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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