Search code examples
c#gdiantialiasingtext-rendering

Render antialiased text using TextRenderer C#


I wrote a program to draw some string on an image. I use graphics.DrawString() but as you can see in this post, it has some problems.
TextRenderer.DrawText() solved that problem but the rendered text is jagged. I changed both graphics.TextRenderingHint and resolution of graphics; TextRenderer doesn't care at all.
There should be a solution as it is done here; but I don't know how to do it.


Solution

  •         private void panel1_Paint(object sender, PaintEventArgs e)
            {
                //GDI (i.e. TextRenderer)
                String s = "The quick brown fox jumped over the lazy dog";
                Point origin = new Point(11, 11);
                Font font = SystemFonts.IconTitleFont;
    
                e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
    
                TextRenderer.DrawText(e.Graphics, s, font, origin, SystemColors.InfoText);
            }
    

    Full demo showing that it does work: https://mega.nz/file/E3xREYIR#kuDxyac_0jxlX7wuTVmZmJgClEicdaCj0YpnE83Wq9k