Width of the text in pixels (Powershell GUI)

Does Powershell have a special method for determining the length of a string in pixels (in this case, the width of the text is determined automatically using ".AutoSize = $True")? Or should I know the width of one character and then multiply by the length of the string? For example, the text has the font Arial 14px.

$StackOverflow = New-Object System.Windows.Forms.Label
$StackOverflow.AutoSize = $True
$StackOverflow.Font = 'Arial, 14px, Style=Regular'
$StackOverflow.Text = 'Thank you for helping me learn the Powershell syntax!'

Ideally, if there is a method similar to this:

$StackOverflow.TextWidth() # Width of the text in pixels

PS> 371

And what if it is for example Chinese?

$StackOverflow.Text = '感謝您幫助我學習Powershell語法。'

Many thanks!

Upvotes: 2

Views: 1649

Answers (1)

Thanks to Mclayton's hints, I found the answer (link) and write it for those who need it.

$String = 'Thank you for helping me learn the Powershell syntax!'
$Font = New-Object System.Drawing.Font('Arial', 14)
$Size = [System.Windows.Forms.TextRenderer]::MeasureText($String, $Font)

$Size.Width

Upvotes: 2

Related Questions