1

I have the following C# code to get the Graphics from a Bitmap on Windows 7 64-bit

protected Graphics GetImageGraphics()
{
    var g = Graphics.FromImage(image);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    g.FillRectangle(backBrush, new Rectangle(Point.Empty, Bounds.Size));
    return g;
}

This is used as follows:

 protected override void Redraw()
 {
    var g = GetImageGraphics();

    var path = RoundedRectanglePath(shapeRect, CornerRadius);
    g.FillPath(selected ? selectedBrush : brush, path);
    g.DrawPath(pen, path);

    foreach(var pin in pins)
        pin.Value.Render(g);

    var labelMax = shapeRect;
    labelMax.Inflate(-10, -10);

    var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak | TextFormatFlags.WordEllipsis;
    TextRenderer.DrawText(g, Task.Symbol.Label, Font, labelMax, Color.Black, fmt);

    fmt = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
    TextRenderer.DrawText(g, Task.TaskId, Font, idRect, Color.Black, fmt);
}

The image created is then copied to the screen using:

g.DrawImageUnscaled(image, Bounds);

The resulting text is very ugly. It is rendered in solid black where each pixel has been replaced by three adjacent horizontal pixels (like an old dot-matrix printer bold font)

If I change the TextRenderingHint to AntiAlias or AntiAliasGridFit then the font is rendered more clearly but without any antialiasing (like SingleBitPerPixel)

What am I doing wrong?

Andy Bisson
  • 580
  • 5
  • 19

1 Answers1

0

The solution is two-fold. As Hans pointed out, text will not antialias into transparent. The other part of the answer is not to use TextRenderer on in-memory BitMaps as described here https://stackoverflow.com/a/1578056/429328

Community
  • 1
  • 1
Andy Bisson
  • 580
  • 5
  • 19