Why are there no exceptions when using a non-existing font?

Possible duplicate:
Check if font is installed

let's say that I used the installed font on the system:

new System.Drawing.Font("Arial", 120F); 

everything is good. Now, if I were to use a non-existing font:

 new System.Drawing.Font("IdoNotExistHaHa", 120F); 

I do not get an exception. As I can see, if I use a font that does not exist, I get a standard font (arial ?, not sure). Be that as it may, I would like to make an exception if there is no font found. How?

0
source share
3 answers

MSDN says the following:

For more information about building fonts, see How to. Create a font family and fonts. Support for Windows Forms TrueType applications and limited OpenType font support. if you try to use a font that is not supported, or a font not installed on the machine on which the application is running, the Microsoft Sans Serif font will be replaced.

You can verify the font is correct by following these steps:

 var myFont = new Font(fontName) if (myFont.Name != fontName ) { throw new Exception() } 
+2
source

You can see this in the documentation itself, Font Designer (String, Single)

Windows Forms applications support TrueType fonts and limited support for OpenType fonts. If the familyName parameter specifies a font that is not installed on the computer on which the application is running, or is not supported, Microsoft Sans Serif will be replaced.

In short, the default font is Microsoft Sans Serif

+2
source

You can check and check if the first font is installed. From Jeff Hillman answer here: Check if the font is installed

 string fontName = "Consolas"; float fontSize = 12; Font fontTester = new Font( fontName, fontSize, FontStyle.Regular, GraphicsUnit.Pixel ); if ( fontTester.Name == fontName ) { // Font exists } else { // Font doesn't exist } 

Obviously, you could throw an exception if you want (since this is your original question), although I would recommend not to do this, and an exception is an expensive operation if you can handle the problem more gracefully.

+1
source

Source: https://habr.com/ru/post/1480572/


All Articles