Graphics g=new Graphics();
 SizeF size= g.MeasureString(str,font);

如上图用法,这么测量出来的长度是不对的,因为方法返回的区域总要大于实际字符串的的宽度

使用下面的方法,即可获得准确的字符串宽度:

Graphics g=new Graphics();
            return (int)g.MeasureString(str, font, 500, StringFormat.GenericTypographic).Width;

参数说明:str: 待测字符串

      font: 字体

      500: 允许测量字符串的最大宽度(待测量的字符串长度如果>500,也返回500)

      StringFormat.GenericTypographic: 系统常量

 

相关文章:

  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
相关资源
相似解决方案