views:

191

answers:

2

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.

A: 

Hi,

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

prashant
It looks like a shadow. If i scale it to 30 points it looks like a shadow.
griegs
+2  A: 

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
    }
}
Reddog
+1 Good tip on the using keyword. Still looking for a solution tho
griegs