title: How to measure the pixel width of a digit in a given font / size (C#)
body: <p>I am trying to calculate the pixel width of Excel columns, as described in <a href="http://www.eggheadcafe.com/software/aspnet/31341329/xlsx-cell-width--in-twip.aspx" rel="nofollow noreferrer">this post</a>, using the official formula from the OpenXML specification. However, in order to apply this formula, I need to know the <em>Maximum Digit Width</em> of the <em>Normal</em> font, which is the pixel width of the widest numeric digit. The OpenXML specification gives this example as a clarification:</p>
  
  <p><em>Using the Calibri font as an example, the maximum digit width of 11 point font
  size is 7 pixels (at 96 dpi).</em></p>
  
  <p>I have checked that this is correct by visually examining a Calibri 11-point digit and it is indeed 7 pixels wide. So, I am trying to create a method that will return the maximum digit width for any font / size.</p>
  
  <p>I have followed the recommendations made in <a href="https://stackoverflow.com/questions/1726197/find-char-width-in-pixels-for-various-arial-fontsizes">this question</a>, but it doesn't produce the results I am expecting.</p>
  
  <p>Here's my test code:</p>
  
  <pre><code>var font = new Font("Calibri", 11.0f, FontStyle.Regular);
  
  for (var i = 0; i &lt; 10; i++)
  {
      Debug.WriteLine(TextRenderer.MeasureText(i.ToString(), font));
  }
  </code></pre>
  
  <p>This reports that all digits have a width of 15.</p>
  
  <p>Any suggestions please?</p>
  
  <p>Thanks,
  Tim</p>