C# DrawString is Adding Outline or Drop Shadow to Certain Fonts at Certain Sizes

332 Views Asked by At

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.

0

There are 0 best solutions below