微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用DrawString绘制没有边框的文本

我有这个代码;

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;

如果有人可以解释为什么我需要这个会很棒.

解决方法

你在那里看到的是正确的.见 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 (var surface = Graphics.FromImage(image))
{
    var font = ... // Build font
    using (var foreBrush = new SolidBrush(Color.Red))
    {
        ... // Do stuff with brush
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐