[UWP] How to capture a Node with transparent.

Aloento 81 Reputation points
2021-01-13T16:56:43.467+00:00

For example, I have a UWP app, which has a node "Button" on it.
Now, I want to make a snap for this Button. And, the output only includes this Button.

For JavaFX, we have the function "node.snapshot()", how about C# with UWP?

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2021-01-14T02:41:36.117+00:00

    Hello,

    Welcome to Microsoft Q&A.

    You could use RenderTargetBitmap class(which represents an image source that can be populated with the combined contents of a XAML visual tree) to do the job.

    For example:

    //Page.xaml   
    <StackPanel>  
        <Button x:Name="capturedButton" Content="capture me" Margin="10"/>  
        <Button x:Name="button" Content="click me" Margin="10" Click="button_Click"/>  
        <Image x:Name="image" Margin="10" Width="200" Height="200" Stretch="None"/>  
    </StackPanel>  
    
    //Page.xaml.cs  
    private async void button_Click(object sender, RoutedEventArgs e)  
    {  
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();  
        await renderTargetBitmap.RenderAsync(capturedButton);  
        image.Source = renderTargetBitmap;  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful