float MeasureString(string stringToPaint, string fontName, int fontSize) { Font font = new Font(fontName, fontSize, FontStyle.Regular); // Set TextFormatFlags to no padding so strings are drawn together. TextFormatFlags flags = TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix; // Declare a proposed size with dimensions set to the maximum integer value. Size proposedSize = new Size(int.MaxValue, int.MaxValue); Size size = TextRenderer.MeasureText( stringToPaint, font, proposedSize, flags); return size.Width; }