public static string ShortenStringToFit(string text, int maxWidth, int originalWidth, Font font, Graphics g)
{
const string kEllipsis = "\u2026";
var txtWidth = g.MeasureString(text, font).Width;
var padding = originalWidth - txtWidth;
while (txtWidth + padding > maxWidth)
{
var len = text.Length - 2;
if (len <= 0)
break; // I can't conceive this happening, but I'm also paranoid.
text = text.Substring(0, len) + kEllipsis; // trim, add ellipsis
txtWidth = g.MeasureString(text, font).Width;
}
return text;
}