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

发布于 2024-07-21 04:44:01 字数 457 浏览 7 评论 0原文

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

我正在尝试:

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);

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

提前致谢 :)

I'd like to load a font from an external server and once is loaded (I guess that's necessary) use it to create a few textfields.

I'm trying:

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);

But it creates the textblock with the default font.
Any ideas?

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-07-28 04:44:01

如果您可以将其加载到流中,请尝试使用 PrivateFontCollection我对另一个问题的回答

编辑:请参阅系统。 Net.WebRequest.GetRequestStream,将 URI 加载到 Stream 中,然后将该 Stream 加载到 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);
                }
            }
        }
    }
}

If you can load it into a Stream, try using a PrivateFontCollection. Example code in my answer to another question.

EDIT: See System.Net.WebRequest.GetRequestStream, load the URI into a Stream, then load that Stream into the PFC as mentioned in the linked code.

Also, I'd save the file locally, and look for it there first, so you don't have to download it every time you run the program.

EDIT AGAIN: Sorry, not WebRequest.GetRequestStream, you want WebResponse.GetResponseStream(). Here's some sample code to do exactly what you're looking for.

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);
                }
            }
        }
    }
}
夏天碎花小短裙 2024-07-28 04:44:01

您传递给 FontFamily 构造函数的系列名称实际上是字体文件公开的系列名称吗? 在您的示例中,如果您加载了 Wingding.ttf,则字体系列名称将为 Wingdings,而不是 bf_helvetica。 如果字体文件是 bf_helvetica.ttf,则系列名称可能不是字体名称,例如 Helvetica 或 Helvetica Bold。

Is the family name you pass to the FontFamily constructor actually a family name exposed by the font file? In your example, if you loaded the Wingding.ttf, the font family name would be Wingdings, not bf_helvetica. If the font file is bf_helvetica.ttf, the family name is likely something other than the name of the font, like Helvetica or Helvetica Bold.

美煞众生 2024-07-28 04:44:01

我发布了一个针对 true type 字体的解决方案,但它可以与其他类型一起使用。

C# 如何添加TTF TO Visual Studio 中的项目

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

我希望它有帮助。

I posted a solution for true type fonts but it could work with other types.

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

I hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文