Exemplo n.º 1
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            webView.NavigationCompleted -= NavigationCompleteB;
            webView.NavigationCompleted += NavigationCompleteC;

            IsMainPageChild(webView);

            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                try
                {
                    var width = (int)webView.GetValue(PngWidthProperty);
                    System.Diagnostics.Debug.WriteLine("B width=[" + width + "]");

                    var contentSize = await webView.WebViewContentSizeAsync();

                    System.Diagnostics.Debug.WriteLine("B contentSize=[" + contentSize + "]");
                    System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");

                    if (contentSize.Height != webView.Height || width != webView.Width)
                    {
                        webView.Width  = contentSize.Width;
                        webView.Height = contentSize.Height;
                        System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");

                        webView.InvalidateMeasure();
                        System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                    }

                    //await Task.Delay(2000);
                    await webView.CapturePreviewToStreamAsync(ms);

                    var decoder = await BitmapDecoder.CreateAsync(ms);

                    var transform = new BitmapTransform
                    {
                        //ScaledHeight = (uint)decoder.PixelHeight,
                        //ScaledWidth = (uint)decoder.PixelWidth
                        ScaledHeight = (uint)Math.Ceiling(webView.Height),
                        ScaledWidth  = (uint)Math.Ceiling(webView.Width)
                    };

                    var pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.RespectExifOrientation,
                        ColorManagementMode.DoNotColorManage);

                    var bytes = pixelData.DetachPixelData();


                    var piclib   = Windows.Storage.ApplicationData.Current.TemporaryFolder;
                    var fileName = (string)webView.GetValue(PngFileNameProperty) + ".png";
                    var file     = await piclib.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);

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

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             //(uint)decoder.PixelWidth, (uint)decoder.PixelHeight,
                                             (uint)Math.Ceiling(webView.Width), (uint)Math.Ceiling(webView.Height),
                                             0, 0, bytes);
                        await encoder.FlushAsync();
                    }

                    webView.Width  = (int)webView.GetValue(BeforeWidthProperty);
                    webView.Height = (int)webView.GetValue(BeforeHeightProperty);
                    webView.Refresh();

                    var toFileResult = new ToFileResult(false, file.Path);
                    webView.SetValue(ToFileResultProperty, toFileResult);
                }
                catch (Exception e)
                {
                    webView.Width  = (int)webView.GetValue(BeforeWidthProperty);
                    webView.Height = (int)webView.GetValue(BeforeHeightProperty);
                    webView.Refresh();

                    var toFileResult = new ToFileResult(true, e.InnerException?.Message ?? e.Message);
                    webView.SetValue(ToFileResultProperty, toFileResult);
                }
            }
            webView.Refresh();
        }