Imports System.Globalization
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Text
Imports System
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FontSizeDemo
public Shared Sub Main
Application.Run(New FontSizesForm)
End Sub
End class
Public Class FontSizesForm
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents listBox1 As System.Windows.Forms.ListBox
Private Sub InitializeComponent()
Me.listBox1 = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(560, 182)
Me.Name = "FontSizesForm"
Me.Text = "FontSizesForm"
Me.ResumeLayout(False)
End Sub
Private Function GetPixelsFromPoints(ByVal points As Single, ByVal dpi As Single) As Single
Return (dpi * points) / 72
End Function
Private Function GetInchesFromPoints(ByVal points As Single, ByVal dpi As Single) As Single
Return GetPixelsFromPoints(points, dpi) / dpi
End Function
Private Function GetPixelsFromDesignUnits(ByVal designUnits As Single, ByVal font As Font, ByVal dpi As Single) As Single
Dim scale As Single = GetPixelsFromPoints(font.Size, dpi) / font.FontFamily.GetEmHeight(font.Style)
Return designUnits * scale
End Function
Private Function GetPointsFromPixels(ByVal pixels As Single, ByVal dpi As Single) As Single
Return (pixels * 72) / dpi
End Function
Private Sub FontSizesForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim g As Graphics = Me.CreateGraphics()
Console.WriteLine("Font.Name= {0}", Me.Font.Name)
Console.WriteLine("Font.FontFamly.Name= {0}", Me.Font.FontFamily.Name)
Console.WriteLine("Font.Size= {0} {1}s (specified Unit)", Me.Font.Size, Me.Font.Unit.ToString())
Console.WriteLine("Font.Height= {0} Pixels", Me.Font.Height)
Console.WriteLine("Font.GetHeight= {0} Pixels", Me.Font.GetHeight(g))
Console.WriteLine("Font.SizeInPoints= {0} Points", Me.Font.SizeInPoints.ToString())
Console.WriteLine("GetPixels(Font.SizeInPoints)= {0} Pixels", GetPixelsFromPoints(Me.Font.SizeInPoints, g.DpiY))
Console.WriteLine("GetInches(Font.SizeInPoints)= {0} Inches", GetInchesFromPoints(Me.Font.SizeInPoints, g.DpiY))
Console.WriteLine("Font.FontFamily.GetEmHeight= {0} Design Units", Me.Font.FontFamily.GetEmHeight(FontStyle.Regular))
Console.WriteLine("GetPixels(GetEmHeight)= {0} Pixels", GetPixelsFromDesignUnits(Me.Font.FontFamily.GetEmHeight(Me.Font.Style), Me.Font, g.DpiY))
Console.WriteLine("Font.FontFamily.GetLineSpacing= {0} Design Units", Me.Font.FontFamily.GetLineSpacing(FontStyle.Regular))
Console.WriteLine("GetPixels(GetLineSpacing)= {0} Pixels", GetPixelsFromDesignUnits(Me.Font.FontFamily.GetLineSpacing(Me.Font.Style), Me.Font, g.DpiY))
Console.WriteLine("Font.FontFamily.GetCellAscent= {0} Design Units", Me.Font.FontFamily.GetCellAscent(FontStyle.Regular))
Console.WriteLine("GetPixels(GetCellAscent)= {0} Pixels", GetPixelsFromDesignUnits(Me.Font.FontFamily.GetCellAscent(Me.Font.Style), Me.Font, g.DpiY))
Console.WriteLine("Font.FontFamily.GetCellDescent= {0} Design Units", Me.Font.FontFamily.GetCellDescent(FontStyle.Regular))
Console.WriteLine("GetPixels(GetCellDescent)= {0} Pixels", GetPixelsFromDesignUnits(Me.Font.FontFamily.GetCellDescent(Me.Font.Style), Me.Font, g.DpiY))
Console.WriteLine("Padding= {0} Design Units", Me.Font.FontFamily.GetLineSpacing(FontStyle.Regular) - (Me.Font.FontFamily.GetCellAscent(FontStyle.Regular) + Me.Font.FontFamily.GetCellDescent(FontStyle.Regular)))
'Console.WriteLine("GetPixels(Padding)= {0} Pixels", GetPixelsFromDesignUnits(Me.Font.FontFamily.GetLineSpacing(FontStyle.Regular) - (Me.Font.FontFamily.GetCellAscent(FontStyle.Regular) + Me.Font.FontFamily.GetCellDescent(FontStyle.Regular)), Me.Font, g.DpiY)))
End Sub
End Class