Using DrawString to Draw Borderless Text

I have this code;

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

However, when the text is drawn in my image, it is red with a black border or shading.

How can I write text on an image without any border or shadow?

EDIT

I solved this by adding:

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

If someone can explain why I need this, it will be great.

+3
source share
3 answers

What you have there seems right. See http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image

You can try TextRenderer.DrawText (although note that this is in the System.Windows.Forms namespace, so this may not be appropriate):

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

Bitmap , . , . (Color.White) .

+3

I think that there should be no border. Is it a border or a shadow? Try another font and font size + FontStyle.Regular and see if there is a difference

0
source

Source: https://habr.com/ru/post/1746316/


All Articles