简体   繁体   English

隐藏xaml元素,但也可以使用UWP中的RenderTargetBitmap进行渲染

[英]Hiding a xaml element but also be able to render using RenderTargetBitmap in UWP

I am using RenderTargetBitmap to capture the content of a control in XAML. 我正在使用RenderTargetBitmap捕获XAML中控件的内容。 I am aware of using Visibility="Collapse" that would hide the control, but when the control is collapsed then RenderTargetBitmap renders a blank image, because according to the docs 我知道使用Visibility =“ Collapse”可以隐藏控件,但是当控件折叠时, RenderTargetBitmap会呈现空白图像,因为根据文档

Content that's in the tree but with its Visibility set to Collapsed won't be captured. 树中但将“可见性”设置为“已折叠”的内容将不会被捕获。

and

Content that can't be captured will appear as blank in the captured image, but other content in the same visual tree can still be captured and will render (the presence of content that can't be captured won't invalidate the entire capture of that XAML composition). 无法捕获的内容将在捕获的图像中显示为空白,但是同一视觉树中的其他内容仍可以捕获并进行渲染(存在无法捕获的内容不会使对的全部捕获无效XAML组成)。

However I don't want to display it on the screen. 但是,我不想在屏幕上显示它。

I was looking for something like a z-index or a layer, so I don't display it but still be able to capture that element. 我正在寻找z-index或图层之类的东西,因此我不显示它,但仍然能够捕获该元素。

Alternatively some other way that does not use RenderTargetBitmap that renders the element even though the visibility is set to collapse 或者,即使visibility设置为collapse ,也可以使用一些不使用RenderTargetBitmap来渲染元素的其他方式

You can use the Grid as it stacks the elements. 您可以在Grid堆叠元素时使用它。 If you place an element at first it will be visually stacked below the second one only if they are in same row or column. 如果将元素放在第一位,则只有它们在同一行或同一列中时,它才会在视觉上堆叠在第二个元素的下方。 In the sample below RedGrid is visualy below the WhiteGrid . 在下面的示例中, RedGrid在WhiteGrid下是可见的 So, you cannot visualy see it. 因此,您看不到它。 But when you use RenderTargetBitmap for RedGrid it will return red colored rectangle as image 但是,当您将RenderTargetBitmap用于RedGrid时 ,它将返回红色矩形作为图像

<Grid >
    <Grid.RowDefinitions>
       <RowDefinition/>
       <RowDefinition/>
    </Grid.RowDefinitions>
        <Grid x:Name="RedGrid" Background="Red" Height="100" Width="100"></Grid>
        <Grid x:Name="WhiteGrid" Background="White" Height="100" Width="100"></Grid>
        <Button Grid.Row="1" Content="Render" Click="Button_Click"></Button>
</Grid>

//C# code // C#代码

 private async void Button_Click(object sender, RoutedEventArgs e)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap();
            await rtb.RenderAsync(GridToBeRendered);

            var pixelBuffer = await rtb.GetPixelsAsync();
            var pixels = pixelBuffer.ToArray();
            var displayInformation = DisplayInformation.GetForCurrentView();
            StorageFolder myfolder = ApplicationData.Current.LocalFolder;
            StorageFile file;
            file = await myfolder.CreateFileAsync("Render" + ".png", CreationCollisionOption.GenerateUniqueName);
            if (file != null)
            {
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Premultiplied,
                                         (uint)rtb.PixelWidth,
                                         (uint)rtb.PixelHeight,
                                         displayInformation.RawDpiX,
                                         displayInformation.RawDpiY,
                                         pixels);
                    await encoder.FlushAsync();
                }
            }
            await Launcher.LaunchFileAsync(file);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM