加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:36:07 所属栏目:百科 来源:网络整理
导读:我有这个代码; 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(t
我有这个代码;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读