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.

Rafael
  • 1,281
  • 2
  • 10
  • 35
  • 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 – Hans Passant Apr 25 '22 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. – AlwaysStuck Apr 25 '22 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... – Ray Apr 26 '22 at 02: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. – AlwaysStuck Apr 27 '22 at 17:44

0 Answers0