【问题标题】:Import a Font into a Windows Server Process将字体导入 Windows Server 进程
【发布时间】:2013-03-28 23:47:23
【问题描述】:

我有一项旨在进行图像处理的服务。其中一项任务是在图像上叠加一个字符串,然后将其发送到程序并显示。

我无法在位图图像上叠加文本,有人可以帮忙吗?

我正在尝试使用:

PrivateFontCollection fonts = new PrivateFontCollection();
public static FontFamily LoadFontFamily(string fileName, out PrivateFontCollection fontCollection)
{
    fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile(HostingEnvironment.ApplicationPhysicalPath + '/' + fileName);
    return fontCollection.Families[0];
}

FontFamily family = LoadFontFamily("arial.ttf", out fonts);
Font font = new Font(family, 20);

using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawString("text", font, new SolidBrush(GetColorFromHexString(foreground)), new PointF(10F, 10F));
}

根据this page上的信息。

由于服务不知道“Arial”是什么 - 它显然失败了。我已将 .ttf 字体作为资源包含在服务中,并将构建内容设置为“始终复制”。

谢谢!

【问题讨论】:

  • 你尝试过任何东西吗?
  • 服务没有 GUI,所以字体没有意义。
  • 我已经更新了问题。
  • 服务器上是否提供 Arial 字体?
  • 另外,检查这个问题:stackoverflow.com/q/544972/395718

标签: c# image-processing fonts gdi


【解决方案1】:

Arial 是最古老的 Windows 字体之一,长期以来,它作为所有 Windows 操作系统版本(包括 Windows 服务器)的默认安装的一部分提供。所以没有理由:

    Font font = new Font("Arial", 20);

不应该工作。

【讨论】:

    【解决方案2】:

    Wcf 与字体无关,建议去掉这个标签。

    我没有错误,但您的图像不包含您的字符串 - 您的逻辑无效。

    我建议以下步骤:

    1. 确保此代码适用于常规 winforms 项目
    2. 尝试将此逻辑重构为单独的 dll(类库,而不是 win 表单)。如果您有 Resharper 之类的工具 - 它会添加所有必需的参考。
      此时 - 您的代码应该适用于所有情况,所以
    3. 将其移回以通过您的 wcf 服务公开。

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2021-05-22
      • 2018-08-18
      • 2018-02-22
      • 2012-05-12
      • 2021-12-20
      • 1970-01-01
      • 2022-12-30
      • 2012-03-03
      相关资源
      最近更新 更多