Measure a String without using a Graphics object?

前端 未结 5 1074
梦谈多话
梦谈多话 2020-11-27 19:57

I am using pixels as the unit for my font. In one place, I am performing a hit test to check if the user has clicked within the bounding rectangle of some text on screen. I

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 20:33

    MeasureString method in @NerdFury answer will give a higher string width than expected.Additional info you can find here. If you want to measure only the physical length please add this two lines :

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    result = 
      g.MeasureString(measuredString, font, int.MaxValue, StringFormat.GenericTypographic);
    

提交回复
热议问题