Hi, I am using RenderTargetBitmap to capture a snapshot of aPage following the instruction of a msdn sample. My problem is: if I put the page on aPopup Control, then RenderTargetBitmap.RenderAsync() would throw ArgumentException(Additional information: "Value does not fall within the expected range"), but if I justNavigate to the page using Frame.Navigate() method, everything would be fine. What's wrong with Popup Control? Below is the code:
BlankPage1.xaml
<Page x:Class="Test.BlankPage1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="600" Height="600"><Button Content="save image" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></Page>
BlankPage1.xaml.cs
namespace Test { public sealed partial class BlankPage1 : Page { public BlankPage1() { this.InitializeComponent(); } public async Task CaptureElementToBitmapAsync(FrameworkElement element, string suggestedFileName) { RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(element); //throw ArgumentException
IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedFileName = suggestedFileName; savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; savePicker.DefaultFileExtension = ".png"; savePicker.FileTypeChoices.Add("png Image", new string[] { ".png" }); StorageFile file = await savePicker.PickSaveFileAsync(); if (file == null) { return; } using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } } private async void Button_Click(object sender, RoutedEventArgs e) { await CaptureElementToBitmapAsync(this, "test"); } } }
Main.xaml.cs
namespace Test { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) {
//method 1: not work BlankPage1 page = new BlankPage1(); Popup popup = new Popup(); popup.Child = page; popup.IsLightDismissEnabled = true; popup.IsOpen = true; //method 2: this would work //this.Frame.Navigate(typeof(BlankPage1)); } } }