我想测量字符串的长度为像素单位。我在网上搜索了2天,但没有运气。最后,我几分钟前从博客中发现了一些代码段,并对其进行了一些修改。这里是我的功能:C#字符串测量
private void cmdMeasure_Click(object sender, EventArgs e)
{
Font fntStyle = new Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel);
Size textSize = TextRenderer.MeasureText(str2measure.Text, fntStyle);
MessageBox.Show(textSize.ToString());
}
问:
什么是16的单位?是em,pt还是GraphicsUnit枚举的单位?我没有真正得到由c#定义的描述,“单位参数指定的单位中的新字体的em尺寸”。
TextRenderer.MeasureText方法是否包含其度量中的字符之间的空格?
继文档[here](http://msdn.microsoft.com/en-us/library/ms141987.aspx)和[here](http://msdn.microsoft.com/en-us/library /system.drawing.graphicsunit.aspx)显示16以像素为单位,因为参数'GraphicsUnit.Pixel'被提供给Font构造函数。 – 2012-04-18 03:47:45
由于GraphicsUnit.Pixel将会像素化,并且对于大多数字体来说意味着大写字母W的高度。字符可以自然重叠,并且测量将返回最终大小,从左侧到右侧加上边缘-填充。你也可以使用Graphics.MeasureCharacterRanges。 – SimpleVar 2012-04-18 03:52:41
http://stackoverflow.com/questions/451903/how-can-i-convert-a-string-length-to-a-pixel-unit – 2012-04-18 03:56:30