首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PrivateFontCollection.AddMemoryFont可以处理WOFF文件吗?

PrivateFontCollection.AddMemoryFont可以处理WOFF文件吗?
EN

Stack Overflow用户
提问于 2012-11-13 21:39:53
回答 1查看 2.1K关注 0票数 3

我正在尝试编写一个web应用程序,该应用程序应该获取字体的链接,并返回以发送的字体格式编写的内容的图像。

我打开一个读取字体文件的流,并将该文件的InPtr发送给PrivateFontCollection.AddMemoryFont,这样我以后就可以在某些图形对象中使用它。当我使用一个有效的.ttf文件时,一切都运行得很好,但是每当我尝试使用.woff字体文件时,我的AddMemoryFont行都会得到'System.IO.FileNotFoundException: file not found‘,即使我可以看到该文件的读取没有错误。

我尝试使用在线转换器将.woff文件转换为.ttf,当我在新的.ttf文件上运行我的代码时,它工作得很好。

使用.woff文件是否有问题,或者我是否需要更改某些内容才能使其正常工作?

代码语言:javascript
复制
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://www.drivehq.com/file/df.aspx/shareID2129391/fileID59377155/arial.ttf"); // Works fine.
//Stream stream = client.OpenRead("http://themes.googleusercontent.com/static/fonts/kiteone/v1/VNHoD96LpZ9rGZTwjozAOvesZW2xOQ-xsNqO47m55DA.woff"); // Doesn't work.
PrivateFontCollection fonts;
FontFamily family = LoadFontFamily(stream, out fonts);

public static FontFamily LoadFontFamily(Stream stream, out PrivateFontCollection fontCollection)
{
    byte[] buffer = null;
    using (MemoryStream ms = new MemoryStream())
    {
        int count = 0;
        do
        {
            byte[] buf = new byte[1024];
            count = stream.Read(buf, 0, 1024);
            ms.Write(buf, 0, count);
        } while (stream.CanRead && count > 0);
        buffer = ms.ToArray();
    }

    var handle = System.Runtime.InteropServices.GCHandle.Alloc(buffer, System.Runtime.InteropServices.GCHandleType.Pinned);

    try
    {
        var ptr = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
        fontCollection = new PrivateFontCollection();
        fontCollection.AddMemoryFont(ptr, buffer.Length);
        return fontCollection.Families[0];
    }
    finally
    {
        handle.Free();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-06 00:47:45

.NET框架不支持WOFF字体的导入,就像Windows XP/Vista/7/8一样。

您必须使用external tool将其转换为TTF,然后导入TTF。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13361732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档