繁体   English   中英

将字体导入Windows Server进程

[英]Import a Font into a Windows Server Process

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

我在将文字叠加在位图图像上时遇到麻烦,有人可以帮忙吗?

我正在尝试使用:

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

根据此页面上的信息。

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

谢谢!

Arial是最古老的Windows字体之一,并且早已作为所有Windows操作系统版本(包括Windows服务器)的默认安装的一部分提供。 因此,没有理由:

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

不应该工作。

Wcf与字体无关,因此建议删除此标记。

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

我建议采取以下步骤:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM