Determine how wide a rendered character is in .NET

后端 未结 7 1407
Happy的楠姐
Happy的楠姐 2021-01-02 01:03

Lets say I render the character \"A\" to the screen with a size 14 font in Arial Regular. Is there a way in C# to calculate how many pixels wide it is?


The wa

7条回答
  •  自闭症患者
    2021-01-02 01:55

    The MeasureString property indicates how wide a field one should use to display a string, allowing some 'slop' for overhangs. In particular, the reported width of "a" plus the reported width of "b" is apt to be much greater than the reported width of "ab". As a rough approximation, I'd suggest that if you want the width of "a", you subtract the width of "||" from the width of "|a|". Note that this will still only be approximate, both because of rounding issues, and also because character widths vary with context. For example, in many fonts, the string "TATATATAT" may appear narrower than "AAAATTTTT" because the A's can nestle under the T's.

提交回复
热议问题