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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...