An inspection of the Font class will reveal that every public property is read-only. This means to change a font’s size, you need to create a new Font object with all the same properties of your current font but with the new size. Here is a handy method to do just that:

static public Font ChangeFontSize( Font font, float fontSize )
{
    if (font != null)
    {
        float currentSize = font.Size;
        if (currentSize != fontSize)
        {
            font = new Font( font.Name, fontSize,
                font.Style, font.Unit,
                font.GdiCharSet, font.GdiVerticalFont );
        }
    }
    return font;
}

For example, to double the size of a label’s font:





label.Font = ChangeFontSize( label.Font, label.Font.Size * 2 );

Graphics Unit

Note the method above uses the same GraphicsUnit (point, pixel, millimeter, etc.) as the original font. You may want to overload this method to also accept a specific unit:

static public Font ChangeFontSize( Font font, float fontSize, GraphicsUnit unit )
{
    if (font != null)
    {
        float currentSize = font.Size;
        if (currentSize != fontSize)
        {
            font = new Font( font.Name, fontSize,
                font.Style, unit,
                font.GdiCharSet, font.GdiVerticalFont );
        }
    }
    return font;
}

For example, to resize a label’s font to 12 pixels:

label.Font = ChangeFontSize( label.Font, 12.0F, GraphicsUnit.Pixel );
Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot