4

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 onto my image it's red with a black border or shadowing.

How can I write text to 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 needed this that would be great.

1
  • How about moving your edit to an answer and selecting it as the best? That's what worked for me. Commented Nov 21, 2012 at 9:07

3 Answers 3

4

When colored text was drawn on a newly created Bitmap object with its default transparent background, it had incomplete black border around it. Solved the problem by using graphics.Clear(Color.White) after initialization.

Sign up to request clarification or add additional context in comments.

1 Comment

That really works, any ideas why does black stroke appear?
3

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

You could try TextRenderer.DrawText (though note that it's in the System.Windows.Forms namespace so this could possibly be inappropriate):

TextRenderer.DrawText(args.Graphics, text, drawFont, ClientRectangle, foreColor, backColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter);

For the backColor try using Color.Transparent.

Also when working with your graphics, brushes, fonts, etc. be sure to wrap them in using statements so as to only keep them around as long as required...

e.g.

using (var surface = Graphics.FromImage(image))
{
    var font = ... // Build font
    using (var foreBrush = new SolidBrush(Color.Red))
    {
        ... // Do stuff with brush
    }
}

1 Comment

+1 Good tip on the using keyword. Still looking for a solution tho
0

I think there should not any border. Are it is border or shadow? Just try with some another font and Font size + FontStyle.Regular and see is there any difference

1 Comment

It looks like a shadow. If i scale it to 30 points it looks like a shadow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.