hawkerm / monaco-editor-uwp

A Windows Runtime Component wrapper around the web-based Monaco Editor.
MIT License
152 stars 36 forks source link

Exception when using RenderTargetBitmap(UWP/C#) #72

Open DmitryBorodiy opened 2 years ago

DmitryBorodiy commented 2 years ago

An exception occurs when I try to take a snapshot of the MonacoEditor UI element.

                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(Editor);
                var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

                StorageFile image = await ApplicationData.Current.LocalFolder.CreateFileAsync("image.png", 
                CreationCollisionOption.ReplaceExisting);

                using (var stream = await image.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                    encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Ignore,
                        (uint)renderTargetBitmap.PixelWidth,
                        (uint)renderTargetBitmap.PixelHeight,
                        DisplayInformation.GetForCurrentView().LogicalDpi,
                        DisplayInformation.GetForCurrentView().LogicalDpi,
                        pixelBuffer.ToArray());

                    await encoder.FlushAsync();

                    await stream.FlushAsync();
                    stream.Dispose();

                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(await image.OpenAsync(FileAccessMode.Read));
               }

Value does not fall within the expected range.System.Private.CoreLib Exception thrown: 'System.ArgumentException' in System.Private.CoreLib.dll

DmitryBorodiy commented 2 years ago

This stopped working after upgrading to beta 0.9.0. I rolled back to 0.8.1.

hawkerm commented 2 years ago

@DmitryBorodiy can you provide more info in the stack trace? Can you turn on native debugging and get more of the native stack from the platform? ArgumentException isn't that helpful here. The control is just a WebView, so I don't really know what would be going on here or what would have changed between versions except maybe offloading the WebView thread.