0

Is there a way to define an external font as a new System.Drawing.FontFamily? I tried using this code:

 FontFamily robotoLight = new FontFamily(new Uri("pack://application:,,,/"), 
                                         "./Fonts/#Roboto Light");

And shows this error:

Cannot convert from System.Uri to string

I realized that I've been using a different reference and the one that accepts Uri is the System.Windows.Media.FontFamily, so it does not work.

I researched and saw that to define custom fonts is to use the PrivateFontCollection.AddFontFile, but it's using an absolute path for the font, as answered here.

My fonts currently resides in a fonts folder inside the project, and is built to be included in the .exe file, so an absolute path is no-go for me.

And I need to use System.Drawing and not System.Windows.Media because I'm creating a customized MessageBox

Community
  • 1
  • 1
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
  • Are you sure that "Roboto Light" is the name of the font and not just the name of the file? Try "./Fonts/#Roboto". – mm8 Apr 07 '17 at 07:18
  • @mm8 I'm sure. I've been using it on other parts of the project. And I checked it by using the installer of the font – Carl Binalla Apr 07 '17 at 07:24
  • Oh, a System.Drawing.FontFamily doesn't understand pack URIs. – mm8 Apr 07 '17 at 07:27
  • @mm8 Yeah, and I can't use `System.Windows.Media.FontFamily` because I need `System.Drawing.FontFamily` – Carl Binalla Apr 07 '17 at 07:28
  • @Downvoter Mind explaining? – Carl Binalla Apr 07 '17 at 07:29
  • Did you read this?: http://stackoverflow.com/questions/556147/how-to-quickly-and-easily-embed-fonts-in-winforms-app-in-c-sharp – mm8 Apr 07 '17 at 07:30
  • @mm8 Nope, I didn't found that question, maybe because I'm using "WPF" as a search query – Carl Binalla Apr 07 '17 at 07:33
  • System.Drawing.FontFamily is not WPF :) – mm8 Apr 07 '17 at 07:38
  • @mm8 Oh, ok then. Guess I was right then, because I have just copied a class that creates a custom message box, and it's using `className : Forms`. Nevertheless, I didn't found that question and I still need to use `System.Drawing` – Carl Binalla Apr 07 '17 at 07:43

1 Answers1

1

System.Drawing.FontFamily doesn't understand pack URIs. Please refer to the following question for an example of how you could use an embedded font with the System.Drawing.FontFamily class:

How to quickly and easily embed fonts in winforms app in C#

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Implemented it, but not sure if it really works. I compared it to another with a gibberish font name to revert it to the default font, and it seems different I tried using a `BoldItalic` version, but it does not accept either `FontStyle.Regular` nor `FontStyle.Bold`. Anyway, thanks for the link. – Carl Binalla Apr 07 '17 at 08:13