I use the following function to draw text with background:
Graphics g, string text, Font font, Brush brushText,
int x, int y, Brush brushBackground)
SizeF size = g.MeasureString(text, font);
g.FillRectangle(brushBackground, x, y, (int)size.Width, (int)size.Height);
g.DrawString(text, font, brushText, x, y);
The problem is that MeasureString doesn't give 100% correct result, and background rectangle is too wide. Odd pixels can contain one additional character. How can I fix this?
TextRenderer gives better results, but I cannot use it, because the same code is used for printing. I can use also API for this, but I am looking for .NET solution.