有類似的需求,這裏是我的方法(假設你不知道字體名稱和字體沒有嵌入)。 如果有人有更好的主意,請不要猶豫,添加一些意見,請!
//Writing bytes to a temporary file.
string tempFontFileLoation = "testFont.ttf";
File.WriteAllBytes(tempFontFileLoation, yourBytesHere);
//Creating an instance of System.Windows.Media.GlyphTypeface.
//From here we will read all the needed font details.
var glyphTypeface = new GlyphTypeface(new Uri(tempFontFileLoation));
//Reading font family name
string fontFamilyName = String.Join(" ", glyphTypeface.FamilyNames.Values.ToArray<string>());
//This is what we actually need... the right font family name, to be able to create a correct FontFamily Uri
string fontUri = new Uri(tempFontFileLoation.Replace(Path.GetFileName(tempFontFileLoation), ""), UriKind.RelativeOrAbsolute).AbsoluteUri + "/#" + fontFamilyName;
//And here is the instance of System.Windows.Media.FontFamily
var fontFamily = new FontFamily(fontUri);
這是不正確的代碼,只要字體可以使用,您傳遞的指針必須保持有效。它不會,垃圾收集器在堆積時會將其分解。谷歌「addmemoryfont marshal.alloccotaskmem」,避免*很多馬車示例,馬上致電Marshal.FreeCoTaskMem()。 –
爲什麼不直接在應用程序中打包'Font'?在這裏閱讀:https://msdn.microsoft.com/library/ms753303(v=vs.100).aspx – Jasper
隨着你給我的網站,我不能讓那個賈斯珀你能幫我嗎? – Moussawi