我想将字符串的长度测量为像素单位。我在网上搜索了两天但没有成功。最后,我几分钟前从博客中找到了一些代码片段,并对其进行了一些修改。这是我的功能:
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 enum的单位?我并没有真正得到 c# 定义的描述“单位参数指定的单位中的新字体的 em 大小。”
TextRenderer.MeasureText 方法在其测量中是否包含字符之间的空格?
最佳答案
What is the unit of 16? Is it em, pt, or the unit of GraphicsUnit enum? I don't really get the description defined by c# "the em-size of the new font in the units specified by the unit parameter."
正如其他人所说,自从您传递 GraphicsUnit.Pixel 以来,它就是像素。
Does the TextRenderer.MeasureText method include the spaces between the characters in its measurement?
是的。它为您提供整个文本 block 周围的边界框的尺寸。
关于C# 字符串测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10202476/