1

I'm using C# DrawString to draw text on an image background which is output as a TIFF for printing.

For some fonts at some sizes, the DrawString method is adding a border around the text. Both of these examples are drawn using the same code, just with different font sizes. The first is 10pt and the second is 12pt.

10 pt Open Sans font: 10 pt Open Sans font

12 pt Open Sans font: 12 pt Open Sans font

On other fonts, this change happens at larger sizes. Like if I use Times New Roman, the outline occurs up until 18pt and then it goes away.

This is how I'm creating the background image (I know I should use "using", just not doing so yet):

protected Image CreateCTextBackground() {
    // TODO Get DPI from template
    Bitmap background = new Bitmap(TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);
    background.SetResolution(360f, 360f);

    Graphics graphics = Graphics.FromImage(background);
    graphics.Clear(Color.Transparent);
    graphics.Save();

    return background;
}

Creating a font:

protected Font CreateFont(TextFeature textFeature) {
    FontFamily fontFamily = new FontFamily(textFeature.FontFamily);
    Font font = new Font(
        fontFamily,
        textFeature.FontSize,
        GraphicsUnit.Point);
    return font;
}

And then I'm drawing on it:

 Image textBackground = CreateCTextBackground();
 Graphics textGfx = Graphics.FromImage(textBackground);
 Brush textBrush = new SolidBrush(fontColor);
            
 log.Debug($"xPos={xPos},yPos={yPos}");
 textGfx.TranslateTransform(xPos, yPos);
            
 textGfx.RotateTransform(270);

 textGfx.DrawString(textFeature.Text, font, textBrush, textGfx.VisibleClipBounds, stringFormat);

I'm not sure what I'm doing wrong. Any advice is appreciated.

4
  • 3
    They are the anti-aliasing pixels, aliasing to Color.Transparent. Which is black with an alpha of 0. Text requires a well-defined background on which the text is rendered to ensure anti-aliasing works. If you can't provide one then you must use TextRenderingHint.SingleBitPerPixel/GridFit Commented Apr 25, 2022 at 19:28
  • Thanks Hans. I actually thought it might be something like that, but thought it might be something else because it only happens at smaller font sizes. I guess that is because the larger fonts don't need to be anti-aliased to look crisp. Commented Apr 25, 2022 at 19:58
  • Hans is right, ClearType doesn't work on transparent backgrounds. I'm not sure about simpler antialias though, and I don't remember if TextRenderer could do it. I may have also created a GraphicsPath from a font and drew that filled to get some kind of antialias, but my GDI times are long ago... Commented Apr 26, 2022 at 2:15
  • We ended up switching to a white background. I wonder how Photoshop does it? I don't have any issues putting anti-aliased text on a transparent background in PS. Another topic for another day. :) Thanks for the feedback to you both. I guess Hans comment is the answer. Commented Apr 27, 2022 at 17:44

0

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.