【问题标题】:Using DrawString to draw text with no border使用 DrawString 绘制没有边框的文本
【发布时间】:2010-05-20 05:44:18
【问题描述】:

我有这个代码;

    public static Image AddText(this Image image, ImageText text)
    {
        Graphics surface = Graphics.FromImage(image);
        Font font = new Font("Tahoma", 10);

        System.Drawing.SolidBrush brush = new SolidBrush(Color.Red);

        surface.DrawString(text.Text, font, brush, new PointF { X = 30, Y = 10 });

        surface.Dispose();
        return image;
    }

但是,当文本被绘制到我的图像上时,它是带有黑色边框或阴影的红色。

如何在没有任何边框或阴影的图像中写入文本?

编辑

我通过添加解决了这个问题;

surface.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

如果有人能解释我为什么需要这个,那就太好了。

【问题讨论】:

  • 如何将您的编辑移至答案并将其选为最佳答案?这对我有用。

标签: c# system.drawing


【解决方案1】:

在使用默认透明背景的新创建的位图对象上绘制彩色文本时,其周围有不完整的黑色边框。初始化后使用 graphics.Clear(Color.White) 解决了问题。

【讨论】:

  • 这确实有效,有什么想法为什么会出现黑色中风?
【解决方案2】:

您所拥有的似乎是正确的。见http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image

您可以尝试 TextRenderer.DrawText(但请注意,它位于 System.Windows.Forms 命名空间中,因此这可能不合适):

TextRenderer.DrawText(args.Graphics, text, drawFont, ClientRectangle, foreColor, backColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter);

对于背景颜色,请尝试使用 Color.Transparent。

此外,在使用图形、画笔、字体等时,请务必将它们包装在 using 语句中,以便仅在需要时保留它们...

例如

using (var surface = Graphics.FromImage(image))
{
    var font = ... // Build font
    using (var foreBrush = new SolidBrush(Color.Red))
    {
        ... // Do stuff with brush
    }
}

【讨论】:

  • +1 关于 using 关键字的好提示。仍在寻找解决方案
【解决方案3】:

我认为不应该有任何边界。 是边框还是阴影? 试试其他字体和字体大小 + FontStyle.Regular 看看有什么区别

【讨论】:

  • 它看起来像一个影子。如果我将它缩放到 30 点,它看起来像一个阴影。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多