如何通过RenderTargetBitmap拍摄多个快照元素?
我使用以下XAML代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" x:Name="pin" Height="72" Width="64" Visibility="Visible" >
<Image Stretch="UniformToFill" x:Name="img"
Source="ms-appx:///Assets/Icons/pin_cluster.png"/>
<Image Stretch="UniformToFill" x:Name="emp"
Source="ms-appx:///Assets/Icons/pin.png"/>
<TextBlock x:Name="cluster_count"
Text="99"
FontSize="12"
TextAlignment="Center"
FontWeight="Bold"
VerticalAlignment="Center"
Foreground="#FFF7F7F7" Margin="37,5,0,52" Height="15"/>
</Grid>
<Button Grid.Column="1" Content="Button" HorizontalAlignment="Stretch"
Click="Button_Click" VerticalAlignment="Top" Margin="0,0,5,0"/>
</Grid>
<StackPanel x:Name="stack" Grid.Row="1" Orientation="Horizontal">
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="100" Height="100"/>
<Image Width="78" Height="100"/>
</StackPanel>
</Grid>
例如C#-code:
private async void Button_Click(object sender, RoutedEventArgs e)
{
IsStart = true;
int tt = 0;
foreach (var element in stack.Children)
{
var imgUI = element as Image;
img.UpdateLayout();
imgUI.UpdateLayout();
var bitmap = new RenderTargetBitmap();
cluster_count.Text = tt++.ToString();
if (tt % 2 == 0)
await bitmap.RenderAsync(pin);
else
await bitmap.RenderAsync(emp);
imgUI.Source = bitmap;
bitmap = null;
pin.UpdateLayout();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
代码的本质是从不同图像源上的网格重绘一个或多个图像。特别是,它计划在地图上用作引脚。
问题是RenderTargetBitmap与RenderAsync的多次使用导致重叠快照。
正如您从代码中看到的那样,我尝试使用图像网格的自由内存,通过垃圾收集器和布局更新,但都没有成功。