测试代码:

string str = "字符串";
var width = TextRenderer.MeasureText(str, this.Font);
var width2 = this.CreateGraphics().MeasureString(str, this.Font);

str = "字符串"时,结果为:

width:{Width=41, Height=12}

width2:{Width=41.06836, Height=15.1875}

AutoSize = true的Label尺寸为41, 12

str = "zifuchuan"时,结果为:

width:{Width=59, Height=12}

width2:{Width=59.68748, Height=13.5}

AutoSize = true的Label尺寸为59, 12

综合上述的结果,我取用TextRenderer.MeasureText作为最终获取字符串像素宽度的方法。

相关文章:

  • 2021-05-22
  • 2021-12-17
  • 2023-02-10
  • 2022-02-09
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-07-01
  • 2021-09-23
相关资源
相似解决方案