gpt4 book ai didi

.net - dotnet : How do you get average character width for a font?

转载 作者:行者123 更新时间:2023-12-04 17:06:10 24 4
gpt4 key购买 nike

Windows 窗体:

对于 System.Drawing有一种方法可以获取字体高度。

Font font = new Font("Arial", 10 , FontStyle.Regular);
float fontHeight = font.GetHeight();

但是您如何获得其他文本指标,例如平均字符宽度?

最佳答案

使用 Graphics.MeasureString 方法

private void MeasureStringMin(PaintEventArgs e)
{

// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);

// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);

// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}

关于.net - dotnet : How do you get average character width for a font?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/308850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com