Bug 7136 - BufferedGraphics doesn't translate the buffer
Summary: BufferedGraphics doesn't translate the buffer
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 08:39 UTC by xanatos
Modified: 2017-09-01 11:55 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 7136 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 08:39:06 UTC
This is an example code (you'll have to add the various usings).

The problem is that the .NET BufferedGraphics uses the X and Y of the Rectangle passed as a parameter and gives to the buffer those coordinates as the origin (so if you do BufferedGraphicsManager.Current.Allocate(e.Graphics, new Rectangle(0, height1, width, height2) the origin of the buffer will be (0, height1) and the last pixel will be (width - 1, height1 + height2 - 1). On .NET if you launch the program you will get the top half of the window red and the bottom half green, on Mono you'll get the top half red and the bottom half of the color of the window.

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class Form1 : Form
    {
        public Form1()
        {
            this.Paint += this.Form1_Paint;
            this.Resize += this.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)))
            {
                using (var br = new SolidBrush(Color.Red))
                {
                    bf.Graphics.FillRectangle(br, new Rectangle(0, 0, width, height1));
                }

                bf.Render();
            }

            int height2 = this.ClientSize.Height - height1;

            using (var bf = BufferedGraphicsManager.Current.Allocate(e.Graphics, new Rectangle(0, height1, width, height2)))
            {
                using (var br = new SolidBrush(Color.Green))
                {
                    // This will partially fix the BufferedGraphics, but look at the two different TextRenderer.DrawText
                    ////bf.Graphics.TranslateTransform(0, -height1);
                    bf.Graphics.FillRectangle(br, new Rectangle(0, height1, width, height2));
                    TextRenderer.DrawText(bf.Graphics, "One", this.Font, new Point(0, height1), Color.White);
                    TextRenderer.DrawText(bf.Graphics, "Two", this.Font, new Point(0, height1), Color.White, TextFormatFlags.PreserveGraphicsTranslateTransform);
                }

                bf.Render();
            }
        }

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