Exemplo n.º 1
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            IsMainPageChild(webView);

            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                try
                {
                    //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
                    };

                    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,
                                             0, 0, bytes);
                        await encoder.FlushAsync();
                    }

                    var onComplete = (TaskCompletionSource <ToFileResult>)webView.GetValue(TaskCompletionSourceProperty);
                    System.Diagnostics.Debug.WriteLine(GetType() + "." + P42.Utils.ReflectionExtensions.CallerMemberName() + ": Complete[" + file.Path + "]");
                    onComplete?.SetResult(new ToFileResult(false, file.Path));
                }
                catch (Exception e)
                {
                    var onComplete = (TaskCompletionSource <ToFileResult>)webView.GetValue(TaskCompletionSourceProperty);
                    onComplete?.SetResult(new ToFileResult(true, e.InnerException?.Message ?? e.Message));
                }
            }
        }
Exemplo n.º 2
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "]");


            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                await webView.CapturePreviewToStreamAsync(ms);

                var decoder = await BitmapDecoder.CreateAsync(ms);

                var transform = new BitmapTransform
                {
                    ScaledHeight = (uint)decoder.PixelHeight,
                    ScaledWidth  = (uint)decoder.PixelWidth
                };

                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,
                                         0, 0, bytes);
                    await encoder.FlushAsync();
                }

                var onComplete = (Action <string>)webView.GetValue(OnCompleteProperty);
                onComplete?.Invoke(file.Path);
            }
        }
 private async void loadWebViewToStream(WebView webview, IRandomAccessStream stream)
 {
     await Task.Delay(TimeSpan.FromMilliseconds(10));
     await webview.CapturePreviewToStreamAsync(stream);
     await stream.FlushAsync();
     stream.Seek(0);
 }
Exemplo n.º 4
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();
        }