gpt4 book ai didi

c# - 如何将字符串长度转换为像素单位?

转载 作者:IT王子 更新时间:2023-10-29 04:17:18 24 4
gpt4 key购买 nike

我有这样一个字符串:

string s = "This is my string";

我正在创建一个 Telerik 报告,我需要定义一个 textbox,它是我的字符串的宽度。但是,大小属性需要设置为单位(像素、点、英寸等)。如何将我的字符串长度转换为像素,以便我可以设置宽度?

编辑:我尝试获取对图形对象的引用,但这是在继承自 Telerik.Reporting.Report 的类中完成的。

最佳答案

不使用控件或表单:

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}

或者在 VB.Net 中:

Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
Dim size As SizeF = graphics.MeasureString("Hello there", New Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point))
End Using

关于c# - 如何将字符串长度转换为像素单位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/451903/

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