c#绘制线条正在为某些尺寸的某些字体添加轮廓或掉落阴影

发布于 2025-01-23 19:54:05 字数 1713 浏览 0 评论 0原文

我正在使用C#牵引线在图像背景上绘制文本,该文本是输出的,作为打印的TIFF。

对于某些尺寸的字体,绘图方法是在文本周围添加边框。这两个示例都是使用相同的代码绘制的,仅具有不同的字体尺寸。第一个是10pt,第二点是12pt。

10 pt开放ront:

12 pt Open sans font:

在其他字体上,此更改发生在较大尺寸。就像我使用新罗马时代的时代一样,轮廓才发生到18pt,然后消失。

这就是我创建背景映像的方式(我知道我应该使用“使用”,只是还没有这样做):

protected Image CreateCTextBackground() {
    // TODO Get DPI from template
    Bitmap background = new Bitmap(TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);
    background.SetResolution(360f, 360f);

    Graphics graphics = Graphics.FromImage(background);
    graphics.Clear(Color.Transparent);
    graphics.Save();

    return background;
}

创建一个字体:

protected Font CreateFont(TextFeature textFeature) {
    FontFamily fontFamily = new FontFamily(textFeature.FontFamily);
    Font font = new Font(
        fontFamily,
        textFeature.FontSize,
        GraphicsUnit.Point);
    return font;
}

然后我在借鉴:

 Image textBackground = CreateCTextBackground();
 Graphics textGfx = Graphics.FromImage(textBackground);
 Brush textBrush = new SolidBrush(fontColor);
            
 log.Debug($"xPos={xPos},yPos={yPos}");
 textGfx.TranslateTransform(xPos, yPos);
            
 textGfx.RotateTransform(270);

 textGfx.DrawString(textFeature.Text, font, textBrush, textGfx.VisibleClipBounds, stringFormat);

我不确定我在做什么错误的。任何建议将不胜感激。

I'm using C# DrawString to draw text on an image background which is output as a TIFF for printing.

For some fonts at some sizes, the DrawString method is adding a border around the text. Both of these examples are drawn using the same code, just with different font sizes. The first is 10pt and the second is 12pt.

10 pt Open Sans font:
10 pt Open Sans font

12 pt Open Sans font:
12 pt Open Sans font

On other fonts, this change happens at larger sizes. Like if I use Times New Roman, the outline occurs up until 18pt and then it goes away.

This is how I'm creating the background image (I know I should use "using", just not doing so yet):

protected Image CreateCTextBackground() {
    // TODO Get DPI from template
    Bitmap background = new Bitmap(TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);
    background.SetResolution(360f, 360f);

    Graphics graphics = Graphics.FromImage(background);
    graphics.Clear(Color.Transparent);
    graphics.Save();

    return background;
}

Creating a font:

protected Font CreateFont(TextFeature textFeature) {
    FontFamily fontFamily = new FontFamily(textFeature.FontFamily);
    Font font = new Font(
        fontFamily,
        textFeature.FontSize,
        GraphicsUnit.Point);
    return font;
}

And then I'm drawing on it:

 Image textBackground = CreateCTextBackground();
 Graphics textGfx = Graphics.FromImage(textBackground);
 Brush textBrush = new SolidBrush(fontColor);
            
 log.Debug(
quot;xPos={xPos},yPos={yPos}");
 textGfx.TranslateTransform(xPos, yPos);
            
 textGfx.RotateTransform(270);

 textGfx.DrawString(textFeature.Text, font, textBrush, textGfx.VisibleClipBounds, stringFormat);

I'm not sure what I'm doing wrong. Any advice is appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文