public enum Unit : int
{
Pixels = 0,
Inches = 100,
Centimeters = 254,
Millimeters = 2540,
Points = 7200,
HalfPoints = 14400,
QuarterPoints = 28800,
DocumentUnits = 30000,
Twips = 144000,
}
public static float GetCharWidth(string MonospacedFontName,
float RequestedFontSizeInPoints, Unit CharWidthUnit)
{
int PointsPerInch = 72;
int UnitFactor = 100;
Bitmap B = new Bitmap(16, 16);
Graphics G = Graphics.FromImage(B);
float PixelsPerInch = G.DpiX;
float GeneratedFontSizeInPoints = (int)(RequestedFontSizeInPoints *
PixelsPerInch) / PointsPerInch * PointsPerInch / PixelsPerInch;
Font F = new Font(MonospacedFontName, GeneratedFontSizeInPoints,
FontStyle.Regular, GraphicsUnit.Point);
int WidthInPixels = TextRenderer.MeasureText(G, " ", F,
Size.Empty, TextFormatFlags.NoPadding).Width;
F.Dispose();
G.Dispose();
B.Dispose();
return (CharWidthUnit == Unit.Pixels) ? WidthInPixels :
WidthInPixels / PixelsPerInch * (int)CharWidthUnit / UnitFactor;
}