title: Dynamically resizing font to fit space while using Graphics.DrawString
body: <p>Does anyone have a tip whereas you could dynamically resize a font to fit a specific area? For example, I have an 800x110 rectangle and I want to fill it with the max size font that would support the entire string I'm trying to display.</p>
  
  <pre><code>Bitmap bitmap = new Bitmap(800, 110);
  
  using (Graphics graphics = Graphics.FromImage(bitmap))
  using (Font font1 = new Font("Arial", 120, FontStyle.Regular, GraphicsUnit.Pixel))
  {
      Rectangle rect1 = new Rectangle(0, 0, 800, 110);
  
      StringFormat stringFormat = new StringFormat();
      stringFormat.Alignment = StringAlignment.Center;
      stringFormat.LineAlignment = StringAlignment.Center;
  
      graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
      graphics.DrawString("Billy Reallylonglastnameinstein", font1, Brushes.Red, rect1, stringFormat);
  } 
  
  bitmap.Save(Server.MapPath("~/Fonts/" + System.Guid.NewGuid() + ".png"));
  </code></pre>
  
  <p>Obviously that whole name won't render in the space provided at the large font size. There has to be a simple way to do this?</p>