Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find font and string metrics in .NET Core?

I need to measure a string width and height in pixels in .NET Core 2.

In .NET Framework can be done using Graphics.MeasureString method, but in .NET Core, Graphics class no longer exists.

I also tried to find a Font or FontFamily class and extract the font size, font ascent and fond descent, but I couldn't find any or something similar.

With the current .NET Core version, that is still under hard work, it is possible to measure a string or to get the font metrics?

like image 827
alex.pulver Avatar asked Jan 29 '23 07:01

alex.pulver


1 Answers

Update 17 November 2017

Windows Compatibility Pack for .NET Core is in preview as of today includes System.Drawing.Common and soon will include System.Drawing package.

It is possible to use CoreCompat.System.Drawing.v2 NuGet package which provides cross platform implementation of System.Drawing assembly based on Mono. I am using it currently on Windows, Linux and macOS in CI runs and so far without major hiccups.

DotNet team has finished already work on porting System.Drawing functionality to .NET Core (see issue #20325 in CoreFX GitHub repo Support Full System.Drawing Functionality on .NET Core and issue #24909 Ship .NET Framework compatibility pack. First preview release is planned for November 2017.

Shipping is planned with .NET Core 2.1.0 version and jointly with .NET Framework Microsoft compatibility pack. Package will support all major platforms with exception of UWP on Windows.

Graphics.MeasureString function is implemented here

like image 83
Jacek Blaszczynski Avatar answered Feb 02 '23 11:02

Jacek Blaszczynski