4

我有这个代码;

    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;

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

4

3 回答 3

4

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

于 2012-04-04T08:21:29.113 回答
3

你所拥有的似乎是正确的。请参阅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);

对于 backColor,请尝试使用 Color.Transparent。

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

例如

using (var surface = Graphics.FromImage(image))
{
    var font = ... // Build font
    using (var foreBrush = new SolidBrush(Color.Red))
    {
        ... // Do stuff with brush
    }
}
于 2010-05-20T06:14:10.010 回答
0

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

于 2010-05-20T05:59:23.043 回答