【问题标题】:embed ttf as embeded resource: can't reference it [duplicate]将ttf嵌入为嵌入式资源:无法引用它[重复]
【发布时间】:2020-12-31 03:39:04
【问题描述】:

我刚刚将一个 ttf 文件作为“文件”添加到项目(c# 2008 express)中,并为嵌入式资源添加了构建选项。

我在尝试像这样设置此字体时遇到问题: (我知道下一行是错的……)

this.label1.Font = AlarmWatch.Properties.Resources.Baby_Universe;

错误 1 ​​无法隐式转换类型 '字节 []' 到 'System.Drawing.Font' C:\Users\hongo\Documents\Visual 工作室 2008\Projects\AlarmWatch\AlarmWatch\Form1.Designer.cs 57 32 AlarmWatch

我知道它是 byte[] 因为我已将选项 build 设置为嵌入式资源,但与此行比较是正确的:

this.label1.Font = new System.Drawing.Font("OCR A Extended",
                           24F, System.Drawing.FontStyle.Regular,
                           System.Drawing.GraphicsUnit.Point, ((byte)(0)));

如何设置 this.label1 以使用新字体?

【问题讨论】:

    标签: c# resources truetype


    【解决方案1】:

    System.Drawing.Text 命名空间中有一个 AddMemoryFont 方法,它从内存中加载字体(它需要一个指向内存块的指针,因此您需要执行一些不安全的操作来获取指向您的字节的指针数组 - 我找到了一个 example here)。更多关于method on MSDN

    还有一个相关的StackOverflow question展示了如何导入Win API函数来直接加载字体(以防上述.NET方法不起作用)。

    编辑 Visual Basic 中关键部分的翻译可能如下所示(但尚未检查):

    // This should be probably a field of some class
    PrivateFontCollection pfc = new PrivateFontCollection();
    
    // allocate memory and copy byte[] to the location
    IntPtr data = Marshal.AllocCoTaskMem(yourByteArray.Length);
    Marshal.Copy(yourFontArray, 0, data, yourFontArray.Length);
    
    // pass the font to the font collection
    pfc.AddMemoryFont(data, fontStream.Length)
    
    // Free the unsafe memory
    Marshal.FreeCoTaskMem(data)
    

    一旦你有了这个,你应该能够使用它的常用名称来引用字体。

    【讨论】:

    • 嗨,感谢您的回答。我去了那个页面,但是我不知道如何将该代码放在我的应用程序中,因为它是在 Visual Basic 中并且不能很好地翻译它......
    • 我得到了正确的名称、大小和样式...但是如果我尝试将字体设置为标签,它不会这样做,而是返回到默认字体。我喜欢这条线 label1.Font = new Font(pfc.Families[0].Name,15f);我将你给我的行放在 form_load 方法中,并尝试在这些行之后的代码块中设置标签....
    【解决方案2】:
    private static void AddFontFromResource(PrivateFontCollection privateFontCollection, string fontResourceName)
    {
        var fontBytes = GetFontResourceBytes(typeof (App).Assembly, fontResourceName);
        var fontData = Marshal.AllocCoTaskMem(fontBytes.Length);
        Marshal.Copy(fontBytes, 0, fontData, fontBytes.Length);
        privateFontCollection.AddMemoryFont(fontData, fontBytes.Length);
        Marshal.FreeCoTaskMem(fontData);
    }
    
    private static byte[] GetFontResourceBytes(Assembly assembly, string fontResourceName)
    {
        var resourceStream = assembly.GetManifestResourceStream(fontResourceName);
        if (resourceStream == null)
            throw new Exception(string.Format("Unable to find font '{0}' in embedded resources.", fontResourceName));
        var fontBytes = new byte[resourceStream.Length];
        resourceStream.Read(fontBytes, 0, (int)resourceStream.Length);
        resourceStream.Close();
        return fontBytes;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      相关资源
      最近更新 更多