I have tried all of the suggested ways to center text, but I can't seem to get the results I want while centering an individual character.
I have a rectangle. In that rectangle I'm drawing a circle with DrawEllipse. Now I want to draw a single character inside the circle using the same rectangle and DrawString, to have it perfectly centered.
Here is my basic code:
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
using (Graphics g = Graphics.FromImage(xImage))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.FillEllipse(fillBrush, imageRect.X, imageRect.Y, imageRect.Width - 1, imageRect.Height - 1);
g.DrawString(Text, font, Brushes.White, imageRect, stringFormat);
}
The text is centered horizontally... but it's not properly centered veritcally. Using a symmetrical character like an uppercase "I", I find that the top of the character is always much nearer to the edge of the rectangle than the bottom of the character. The distance is probably at least a 50% increase.
I assume that it is measuring enough space for characters like a lowercase "j" which hangs lower. However, since I am trying to create a graphical icon with a single letter, I want more precise centering.