Reputation: 1700
So I need to add a .otf font file to a PrivateFontCollection in c# using a font.otf file that is included in my project. While the file loads locally, I receive the following exception when I deploy to my server.
[Exception: Could not find font at path: D:\Inetpub\MyApplication\Content\Fonts\ProximaNovaAlt-Black.otf The exception is: System.IO.FileNotFoundException: File not found. at System.Drawing.Text.PrivateFontCollection.AddFontFile(String filename)
However when I look on the web server and navigate to that exact directory the file is there and is not read-only.
The font file is location in my project as follows /Content/Fonts/ProximaNovaAlt-Black.otf
Also, the ProximaNovaAlt-Black.otf file's properties are as follows.
Finally, here is the code that is throwing the error
public Font GetFont(float fontSize)
{
string path = HttpContext.Current.Server.MapPath(AppDomain.CurrentDomain.BaseDirectory + @"Content\Fonts\ProximaNovaAlt-Black.otf");
try
{
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(path);
return new Font(privateFontCollection.Families[0], fontSize);
}
catch (Exception ex)
{
throw new Exception("Could not find font at path: " + path + " The exception is: " + ex.ToString());
}
}
Upvotes: 3
Views: 6591
Reputation: 3581
Has windows encrypted the font folder on the server machine? I had that happen once. It was quite odd. Windows decided to encrypt the folder for funsies.
If the folder is encrypted, the folder name in windows explorer should be green rather than black.
Upvotes: 0
Reputation: 8868
Try this: Add the font like a resources, then
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
var memory = IntPtr.Zero;
try
{
memory = Marshal.AllocCoTaskMem(value.Length);
Marshal.Copy(value, 0, memory, value.Length);
privateFontCollection .AddMemoryFont(memory, value.Length);
}
finally
{
Marshal.FreeCoTaskMem(memory);
}
return new Font(privateFontCollection.Families[0], fontSize);
this work fine to me.
Upvotes: 2
Reputation: 729
FileNotFoundException is also the exception thrown if the font file is an unsupported type (http://msdn.microsoft.com/en-us/library/system.drawing.text.privatefontcollection.addfontfile.aspx). TrueType fonts have good support, but there is only limited support for OpenType. And in my experience this seems to vary by platform.
I converted my OpenType fonts to TrueType using http://everythingfonts.com/otf-to-ttf and that solved the problem.
Upvotes: 13