代码之家  ›  专栏  ›  技术社区  ›  ozke

加载外部字体并在C中使用#

  •  2
  • ozke  · 技术社区  · 17 年前

    我想从外部服务器加载字体,一旦加载(我想这是必要的),就使用它来创建一些文本字段。

    我正在努力:

    font_uri = new Uri("http://localhost/assets/fonts/wingding.ttf");
    bf_helvetica = new FontFamily(font_uri, "bf_helvetica");
    
    TextBlock test_tb = new TextBlock();
    test_tb.Text = "This is a test";
    test_tb.FontSize = 16;
    test_tb.Foreground = Brushes.Red;
    test_tb.FontFamily = bf_helvetica;
    stage.Children.Add(test_tb);
    

    但它使用默认字体创建文本块。 有什么想法吗?

    事先谢谢:)

    3 回复  |  直到 15 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    如果可以将其加载到流中,请尝试使用 PrivateFontCollection . 实例代码 my answer to another question .

    编辑 System.Net.WebRequest.GetRequestStream ,将URI加载到流中,然后将该流加载到pfc中,如链接代码中所述。

    另外,我将在本地保存文件,并首先在那里查找它,这样您就不必每次运行程序时都下载它。

    再次编辑 :抱歉,不是WebRequest.GetRequestStream,您需要WebResponse.GetResponseStream()。下面是一些示例代码,可以精确地执行您要查找的内容。

    using System;
    using System.Drawing;
    using System.Drawing.Text;
    using System.IO;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace RemoteFontTest
    {
        public partial class Form1 : Form
        {
            readonly PrivateFontCollection pfc = new PrivateFontCollection();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                WebRequest request = WebRequest.Create(@"http://somedomain.com/foo/blah/somefont.ttf");
                request.Credentials = CredentialCache.DefaultCredentials;
    
                WebResponse response = request.GetResponse();
    
                using (Stream fontStream = response.GetResponseStream())
                {
                    if (null == fontStream)
                    {
                        return;
                    }
    
                    int fontStreamLength = (int)fontStream.Length;
    
                    IntPtr data = Marshal.AllocCoTaskMem(fontStreamLength);
    
                    byte[] fontData = new byte[fontStreamLength];
                    fontStream.Read(fontData, 0, fontStreamLength);
    
                    Marshal.Copy(fontData, 0, data, fontStreamLength);
    
                    pfc.AddMemoryFont(data, fontStreamLength);
    
                    Marshal.FreeCoTaskMem(data);
                }
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                using (SolidBrush brush = new SolidBrush(Color.Black))
                {
                    using (Font font = new Font(pfc.Families[0], 32, FontStyle.Regular, GraphicsUnit.Point))
                    {
                        e.Graphics.DrawString(font.Name, font, brush, 10, 10, StringFormat.GenericTypographic);
                    }
                }
            }
        }
    }
    
        2
  •  0
  •   KnownIssues    17 年前

    传递给fontFamily构造函数的族名称实际上是字体文件公开的族名称吗?在您的示例中,如果您加载了wingding.ttf,那么字体系列名称将是wingdings,而不是bf_Helvetica。如果字体文件是bf畗helvetica.ttf,则姓氏可能与字体名称不同,如helvetica或helvetica-bold。

        3
  •  0
  •   HoNgOuRu    15 年前

    我发布了一个真正的字体解决方案,但它可以与其他类型。

    C# HOW TO ADD A TTF TO the project in Visual Studio

    http://hongouru.blogspot.com/2010/10/c-how-to-add-fonts-ttf-true-type-fonts.html

    希望能有所帮助。

    推荐文章