title: how to measure width of a string precisely?
body: <p>First of all, question <a href="https://stackoverflow.com/questions/9957982/how-to-measure-width-of-character-precisely"><code>How to measure width of character precisely?</code></a> which is answered, <em>doesn't really help for this case, so this isn't a duplicate of that</em>.</p>
  
  <p>I have a string. I draw using <code>graphics.DrawString</code>, however when I need to put another one after it, I need to know the precise width of previous string.</p>
  
  <p>For this I use graphics.MeasureString with:</p>
  
  <pre><code>StringFormat format = new StringFormat(StringFormat.GenericTypographic);
  format.Alignment = StringAlignment.Center;
  format.Trimming = StringTrimming.None;
  format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
  </code></pre>
  
  <p>I have tried many other functions, just as <code>TextRendered.MeasureText</code> however all of them fail, with all possible combinations of parameters.</p>
  
  <p>the mentioned combination of MeasureString is most close to what I need (it works in most cases, except for special characters), however using characters like # break it. The width is either shorter or longer.</p>
  
  <p>Is there a way to get a precise size of text produced by DrawString function? How does the DrawString calculate the size of drawing area? It must be clearly some other function because the size always differ.</p>
  
  <p>The source code of whole application is here <a href="https://gitorious.org/pidgeon/pidgeon-main/" rel="nofollow noreferrer">https://gitorious.org/pidgeon/pidgeon-main/</a> (File where I work with this, is <a href="https://gitorious.org/pidgeon/pidgeon-main/blobs/master/scrollback/SBABox.cs" rel="nofollow noreferrer">https://gitorious.org/pidgeon/pidgeon-main/blobs/master/scrollback/SBABox.cs</a>) </p>