Microsoft.Maui.Graphics icon indicating copy to clipboard operation
Microsoft.Maui.Graphics copied to clipboard

[Bug] Windows, WinForms: RestoreState does not work as expected

Open kucint opened this issue 4 years ago • 0 comments

Description

I created custom IDrawable class in order to draw on Canvas. I attached it to GraphicsView in both Maui App and WinForms App. Under Maui it works correctly, but under WinForms the drawing seems to be incorrect.

Steps to Reproduce

  1. Start VS2022 preview
  2. Create the WinForms App under dotnet6
  3. Add reference to Microsoft.Maui.Graphics.GDI.Winforms project. This project is still not published as NuGet so you have to clone it directly from GitHub and add reference manually.
  4. Add reference to Microsoft.Maui.Graphics NuGet (Version="6.0.101-preview.11.758) or alternatively clone the project from GitHub.
  5. Create Drawable class as specified below and attach it to GraphicsView.
public class Drawable : IDrawable
{
    public void Draw(ICanvas canvas, RectangleF dirtyRect)
    {
        RectangleF r = new RectangleF(0, 0, 100, 20);
        for (int i = 0; i < 4; i++)
        {
            canvas.SaveState();
            canvas.Translate(25, 25);
            canvas.RestoreState();

            canvas.DrawRectangle(r);
        }
    }
}

Expected Behavior

  • Because Translate() is between SaveState() and RestoreState(), it should have no influence on drawn rectangles.
  • All rectangles shall be drawn in the same place.

image

Actual Behavior

  • RestoreState() seems to do nothing
  • Consecutive translations are not reset and they accumulate -> Rectangles are drawn translated one after another
  • if you comment RestoreState() -> //RestoreState() the drawing is not affected by the change as Reset() would do nothing

image

Additional Info

I use exactly the same Drawable class in Maui under Windows and it works correctly. Maybe the fact that Maui is using Microsoft.Graphics.Win2D under the hood and WinForms uses System.Drawing.Graphics makes a difference?

kucint avatar Dec 30 '21 22:12 kucint