<p>I have a MemoryStream with the contents of a Font File (.ttf) and I would like to be able to create a FontFamily WPF object from that stream <strong>WITHOUT</strong> writing the contents of the stream to disk. I know this is possible with a System.Drawing.FontFamily but I cannot find out how to do it with System.Windows.Media.FontFamily.</p>
<p>Note: I will only have the stream, so I can't pack it as a resource in the application and because of disk permissions issues, will not be able to write the font file to disk for reference as &quot;content&quot;</p>
<p><strong>UPDATE:</strong></p>
<p>The <a href="https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.fontfamily?view=netframework-4.6.1" rel="nofollow noreferrer">API docs</a> how describe how an application resource can be used, though it is not clear to me whether that is an Embedded resource in the assembly or a file on disk.</p>
<blockquote>
<p>You can use a base URI value when you reference a font that is packaged as part of the application. For example, the base URI value can be a &quot;pack://application&quot; URI, which lets you reference fonts that are packaged as application resources. The following code example shows a font reference that is composed of a base URI value and a relative URI value.</p>
</blockquote>

[{'answer_id': 7336238, 'author': 'kobi7', 'author_id': 588613, 'author_profile': 'https://Stackoverflow.com/users/588613', 'pm_score': 1, 'selected': False, 'text': '<p>The best approach I could think of, was to save the oldFont to a temp directory, and immediately load it using the newFont constructor that accepts a uri.</p>\n'}, {'answer_id': 65662363, 'author': 'Patrick Klug', 'author_id': 10779, 'author_profile': 'https://Stackoverflow.com/users/10779', 'pm_score': 2, 'selected': False, 'text': '<p>There is a similar question <a href="https://stackoverflow.com/questions/44912480/c-sharp-wpf-how-to-load-a-fontfamily-from-a-byte-array">here</a>, which contains a supposed solution by converting a System.Drawing.FontFamily to a WPF font family, all in memory without any file IO:</p>\n<pre><code>public static void Load(MemoryStream stream)\n{\n    byte[] streamData = new byte[stream.Length];\n    stream.Read(streamData, 0, streamData.Length);\n    IntPtr data = Marshal.AllocCoTaskMem(streamData.Length); // Very important.\n    Marshal.Copy(streamData, 0, data, streamData.Length);\n    PrivateFontCollection pfc = new PrivateFontCollection();\n    pfc.AddMemoryFont(data, streamData.Length);\n    MemoryFonts.Add(pfc); // Your own collection of fonts here.\n    Marshal.FreeCoTaskMem(data); // Very important.\n}\n\npublic static System.Windows.Media.FontFamily LoadFont(int fontId)\n{\n    if (!Exists(fontId))\n    {\n        return null;\n    }\n    /*\n    NOTE:\n    This is basically how you convert a System.Drawing.FontFamily to System.Windows.Media.FontFamily, using PrivateFontCollection.\n    */\n    return new System.Windows.Media.FontFamily(MemoryFonts[fontId].Families[0].Name);\n}\n</code></pre>\n<p>This seems to use the <code>System.Drawing.PrivateFontCollection</code>(<a href="https://learn.microsoft.com/en-us/dotnet/api/system.drawing.text.privatefontcollection?view=dotnet-plat-ext-5.0" rel="nofollow noreferrer">^</a>) to add a <code>System.Drawing.Font</code> created from a <code>MemoryStream</code> and then use the <code>Families[0].Name</code> of that font to pass into the <code>System.Windows.Media.FontFamily</code> constructor. I assume the family name would then be a URI to the instance of that font in the  PrivateFontCollection but you\'d probably have to try it out.</p>\n'}]
['https://Stackoverflow.com/questions/44394', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4572/']