/// <summary> /// /// </summary> /// <param name="GraphicRef"></param> /// <param name="GraphicString"></param> /// <param name="clipRectangle"></param> /// <param name="fontResourceName">Either Font Family Name or the full path to the Font Resourse (RESX)</param> /// <param name="MaxFontSize"></param> /// <param name="MinFontSize"></param> /// <param name="SmallestOnFail"></param> /// <returns></returns> static public Font GetAdjustedFont(System.Drawing.Graphics GraphicRef, string GraphicString, System.Drawing.Rectangle clipRectangle, string fontResourceName, int MaxFontSize = 100, int MinFontSize = 10, bool SmallestOnFail = true) { bool privateFont = fontResourceName.ToLower().EndsWith("ttf"); Font OriginalFont = null; try { if (privateFont) { OriginalFont = GetFontFromResx(fontResourceName); } else { OriginalFont = new Font(fontResourceName, 100); } // We utilize MeasureString which we get via a control instance for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--) { Font TestFont; if (privateFont) { TestFont = new Font(private_fonts.Families[0], AdjustedSize); } else { TestFont = new Font(fontResourceName, AdjustedSize); } // Test the string with the new size SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont); if (clipRectangle.Width - 4 > Convert.ToInt32(AdjustedSizeNew.Width) && clipRectangle.Height - 4 > Convert.ToInt32(AdjustedSizeNew.Height)) { // Good font, return it return(TestFont); } else { TestFont.Dispose(); } } // If you get here there was no fontsize that worked // return MinimumSize or Original? if (SmallestOnFail) { return(new Font(OriginalFont.Name, MinFontSize, OriginalFont.Style)); } else { return(OriginalFont); } } finally { if (OriginalFont != null) { OriginalFont.Dispose(); } } }