microsoft / Win2D

Win2D is an easy-to-use Windows Runtime API for immediate mode 2D graphics rendering with GPU acceleration. It is available to C#, C++ and VB developers writing apps for the Windows Universal Platform (UWP). It utilizes the power of Direct2D, and integrates seamlessly with XAML and CoreWindow.
http://microsoft.github.io/Win2D
Other
1.82k stars 287 forks source link

System.InvalidOperationException when drawing using CanvasVirtualControl #746

Closed daljit97 closed 4 years ago

daljit97 commented 4 years ago

So I have a CanvasVirtualControl in my app that I am using to draw ink over, using a standard InkCanvas I retrieved a InkSynchronizer. Then when the strokes are collected, I invalidate the bounding rect of the stroke inside my CanvasVirtualControl and then handle the RegionsInvalidated event as follows:

        private void canvasControl_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args)
        {
            foreach (var region in args.InvalidatedRegions)
            {
                using (var ds = sender.CreateDrawingSession(region))
                {
                    List<InkStroke> strokesToDraw = new List<InkStroke>();

                    foreach (var stroke in inkStrokeContainer.GetStrokes())
                    {
                        var regionRect = region;
                        regionRect.Intersect(stroke.BoundingRect);

                        if(!regionRect.IsEmpty)
                        {
                            strokesToDraw.Add(stroke);
                        }
                    }

                    this.wetInkStrokes = null;

                    System.Diagnostics.Debug.WriteLine("Strokes to draw " + strokesToDraw.Count);
                    ds.DrawInk(strokesToDraw);

                    if (strokesToDraw.Count != 0)
                    {
                        this.inkSync.EndDry(); // exceptions gets thrown here (BeginDry() is called in onStrokesCollected
                    }
                }
            }
        }

This seems to work ok, but when I can change the DPI of my CanvasVirtualControl I get the following error:

System.InvalidOperationException HResult=0x8000000E Message=A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E) Source=Windows StackTrace: at Windows.UI.Input.Inking.InkSynchronizer.EndDry() at CanvasTesting.MainPage.canvasControl_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args) in

daljit97 commented 4 years ago

Ok I solved the issue by moving the call for EndDry() outside the regions_invalidated method.