How to color individual characters and maintain proper spacing / kerning / alignment?

邮差的信 提交于 2019-12-02 01:35:10

GDI+'s DrawString is padding the result which explains the left offset you get.

Try to use the GDI to measure the string. Luckily this is implemented and easy to reach from .Net through the TextRenderer() object, f.ex:

Size sz = TextRenderer.MeasureText(character, 
                                   font, 
                                   new Point(Integer.MaxValue, Integer.MaxValue),
                                   TextFormatFlags.NoPadding);

You can also look into the TextRenderer.DrawText().

Of course, you can always modify your StringFormat instance to use center alignment and DrawString with a rectangle instead of point.

StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Rectangle cr = new Rectangle(x, y, w, h); //same as the colored region
g.DrawString(character, font, brush, cr, sf);

Hope this helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!