This class encapsulates a particular visual representation of a
textual character set.
A Font is a resource-based object, and its
lifetime therefore needs careful management. As with other such
objects, you should Dispose() the object when you
are finished with it to release the resources back to the system.
There are a number of constructors that allow you to create a
Font from a particular
FontFamily, or with a particular face
Name (represented as a string) and of a particular
Size. From the GraphicsUnit
enumeration, you can choose which Unit is used to
measure the size. Typically, this might be
GraphicsUnit.Point or
GraphicsUnit.Pixel. In addition, you can specify a
variety of Style attributes, including
Bold, Italic,
Strikeout, and Underline. The
style elements can be queried individually using the Boolean
properties provided, or by examining the FontStyle
object returned from the Style property.
However, it is not possible to set any of these values and change the
Font. If you wish to create a new object based on an existing
Font but with a different style, you should use
the constructor that takes a prototype Font and a
new FontStyle (perhaps then calling
Dispose() on the original to release it if it is
no longer needed). Similarly, to create an object sharing a
FontFamily, you can use the constructor that takes
a FontFamily and a size.
There are also a number of static members that allow you to create a
Font from various entities in interop scenarios,
including FromHdc(), FromHfont(), and FromLogFont(). There are
comparable methods to convert ToHfont() and
ToLogFont().
To draw a string in a particular Font, you can use
the Graphics.DrawString() method. You can also
call the GetHeight() method to determine the
height a particular font would be if it were rendered on a specific
Graphics surface. However, there is no method to
retrieve the average character width as there is in the Win32 API.
Instead, you should use the Graphics.MeasureString() method to determine the bounding rectangle of a
particular string as it would be rendered on that surface.
Font objects are also used pervasively throughout
the frameworks wherever text is rendered.
public sealed class Font : MarshalByRefObject :
ICloneable, System.Runtime.Serialization.ISerializable, IDisposable {
// Public Constructors
public Font(FontFamily family, float emSize);
public Font(FontFamily family, float emSize, FontStyle style);
public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit);
public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet);
public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont);
public Font(FontFamily family, float emSize, GraphicsUnit unit);
public Font(Font prototype, FontStyle newStyle);
public Font(string familyName, float emSize);
public Font(string familyName, float emSize, FontStyle style);
public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit);
public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet);
public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont);
public Font(string familyName, float emSize, GraphicsUnit unit);
// Public Instance Properties
public bool Bold{get; }
public FontFamily FontFamily{get; }
public byte GdiCharSet{get; }
public bool GdiVerticalFont{get; }
public int Height{get; }
public bool Italic{get; }
public string Name{get; }
public float Size{get; }
public float SizeInPoints{get; }
public bool Strikeout{get; }
public FontStyle Style{get; }
public bool Underline{get; }
public GraphicsUnit Unit{get; }
// Public Static Methods
public static Font FromHdc(IntPtr hdc);
public static Font FromHfont(IntPtr hfont);
public static Font FromLogFont(object lf);
public static Font FromLogFont(object lf, IntPtr hdc);
// Public Instance Methods
public object Clone(); // implements ICloneable
public void Dispose(); // implements IDisposable
public override bool Equals(object obj); // overrides object
public override int GetHashCode(); // overrides object
public float GetHeight();
public float GetHeight(Graphics graphics);
public float GetHeight(float dpi);
public IntPtr ToHfont();
public void ToLogFont(object logFont);
public void ToLogFont(object logFont, Graphics graphics);
public override string ToString(); // overrides object
// Protected Instance Methods
protected override void Finalize(); // overrides object
}