C# Winforms. Drawing text accurately centrally within a rectangle
<p>What I'm attempting to do is draw a number inside a circle so that it's positioned centrally both vertically and horizontally. I'm drawing both the circle and text string using the same rectangle structure and using center for both horizontal and vertical alignments. But, as you can see from the image where I drew a horizontal line across the white circle, the text is aligning its base line centrally and my eye says it's horizontally aligning slightly to the left.</p>

<p>I've tried fudging by adding string.height/2 to the top of the rectangle to shift it down 1/2 its height but it's still not correct. (I adjust the font size so the number will fit within the box according to the number of digits in the counter)</p>

<p>How can I do this properly please?</p>

<pre><code>var rect = new Rectangle(bmpWidth - maxCircleDiameter, bmpHeight - maxCircleDiameter, maxCircleDiameter, maxCircleDiameter);
g.FillEllipse(Brushes.White, rect);

using (var format = NewClassFactory.GetFormat(StringAlignment.Center, StringAlignment.Center, StringTrimming.Character))
  g.DrawString(toDisplay, newFont, Brushes.Black, rect, format);
</code></pre>

<p><a href="https://i.stack.imgur.com/nC0t5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nC0t5.png" alt="enter image description here"></a></p>