Bug 7147 - BufferedGraphics, TextRenderer.DrawText and antialias
Summary: BufferedGraphics, TextRenderer.DrawText and antialias
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: 2.10.x
Hardware: PC Windows
: Lowest normal
Target Milestone: Community
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-09-13 11:16 UTC by xanatos
Modified: 2017-09-01 11:56 UTC (History)
1 user (show)

Tags: mono-community
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 7147 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description xanatos 2012-09-13 11:16:18 UTC
On .NET BufferedGraphics + TextRenderer.DrawText does antialias, on Mono it doesn't. This probably because on Mono BG is based on the Bitmap class, and on Bitmap both in Mono and .NET TextRenderer.DrawText doesn't antialias with the existing background. 

Sample code:

    public class Form1 : Form
    {
        public Form1()
        {
            this.ShowIcon = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Paint += new PaintEventHandler(Form1_Paint);
            this.Resize += new EventHandler(Form1_Resize);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int width = this.ClientSize.Width;
            int height1 = this.ClientSize.Height / 2;

            using (var bf = BufferedGraphicsManager.Current.Allocate(e.Graphics, new Rectangle(0, 0, width, height1)))
            {
                DrawText(bf.Graphics, width, height1);
                bf.Render();
            }

            using (var bm = new Bitmap(width, height1, e.Graphics))
            {
                DrawText(Graphics.FromImage(bm), width, height1);
                e.Graphics.DrawImageUnscaled(bm, new Point(0, height1));
            }
        }

        private void DrawText(Graphics graphics, int width, int height)
        {
            using (var br = new SolidBrush(Color.Red))
            {
                graphics.FillRectangle(br, new Rectangle(0, 0, width, height));
            }

            using (var br2 = new SolidBrush(Color.White))
            {
                graphics.DrawString("Two", this.Font, br2, new Point(0, 0));
            }

            TextRenderer.DrawText(graphics, "Two", this.Font, new Point(0, 14), Color.White);

            TextRenderer.DrawText(graphics, "Two", this.Font, new Point(0, 28), Color.White, Color.Red);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

There are two "groups" of strings:

First group written on a BufferedGraphics, second group written on a Bitmap (and then copied on the window). Each group has three lines of text: Graphics.DrawString, TextRenderer.DrawText (without hint on the background color), TextRenderer.DrawText (with hint on the background color).

On .NET the only wrongly antialiased string is the Bitmap + without hint. On Mono both the BG + no hint and the Bitmap + no hint are wrongly antialiased.