Get font width/height in pixels

Posted in Fonts

This example gets Form.Font width and height. Use TextWidth and TextHeight methods of TCanvas type.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:='Width is '+
    IntToStr(Form1.Canvas.TextWidth(Edit1.Text))+
    ' pixels';
  Label2.Caption:='Height is '+
    IntToStr(Form1.Canvas.TextHeight(Edit1.Text))+
    ' pixels';;
end;