0

i am trying to measure single chars of different fonts from Google fonts. I tried a lot, but the proportion of height and width was always wrong... Here are some of my attempts:

  1. TextRenderer

font:= the bloburi of a Google font-example: "https://github.com/FontFaceKit/open-sans/blob/gh-pages/fonts/Bold/OpenSans-Bold.ttf?raw=true"

        FontRequestHelper helper = new FontRequestHelper(font);
        iFontCollection = helper.iFontCollection;
        FontStyle style = iFontCollection.Families[0].IsStyleAvailable(FontStyle.Bold) ? FontStyle.Bold : FontStyle.Regular;
        System.Drawing.Font googlefont = new System.Drawing.Font(iFontCollection.Families[0], tSize, FontStyle.Regular);
        size = TextRenderer.MeasureText("A", googlefont);
  1. MeasureString

i tried different ones, always not accurated...

      SizeF sizeFf = new SizeF();
      using (var image = new Bitmap(1, 1))
       {
            using (var g = Graphics.FromImage(image))
            {
                sizeFf = g.MeasureString("A", googlefont);
            }
        }

or

        using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
        {
            sizeFf = g.MeasureString("A", googlefont);
        }

I dont need the exact width and height, the right proportion would be also reasonable. Online i only find this two solutions as good... Why does it not work for me?

Thx a lot

Maermot
  • 245
  • 1
  • 3
  • 8
  • If they are not monospace font then measuring any character will give you different result, try measuring W for example in most fonts it is wider than A, and font reserves some space on both sides. Check for example this question http://stackoverflow.com/questions/1833062/how-to-measure-the-pixel-width-of-a-digit-in-a-given-font-size-c. – csharpfolk Jan 12 '16 at 20:29
  • yea i know that, and thats why i need the exact proportional of every char in every font. But my methods given the wrong width/height prop, if i rechecked a font in paint.NET f. e.... – Maermot Jan 12 '16 at 20:53
  • then I can only thing of rendering character to bitmap and finding bounding box, slow but may be accurate when you use big font size. – csharpfolk Jan 12 '16 at 21:54

0 Answers0