I have a huge memory use progress when loading a tif frames into dynamically created Image controls, that's logic, But that used memory dosen't unload when I remove my Image controls ! and that's what I don't understand. here is my test code: XAML:
<Grid>
<Button Content="Load" Margin="0,0,582,280" Click="OnLoadClck"/>
<ScrollViewer Margin="10,54,0,0" HorizontalScrollBarVisibility="Visible">
<StackPanel Orientation="Horizontal" x:Name="panel">
</StackPanel>
</ScrollViewer>
<Button Content="Unload" Margin="147,0,435,280" Click="OnUnloadClick"/>
</Grid>
Code behinde:
private void OnLoadClck(object sender, RoutedEventArgs e)
{
TiffBitmapDecoder tbd = new TiffBitmapDecoder(new Uri("d:\\test.tif"), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
for (int i = 0; i < tbd.Frames.Count; i++)
{
var f = tbd.Frames[i];
Image img = new Image{Width=100, Height=150 };
img.Source = f;
panel.Children.Add(img);
}
}
private void OnUnloadClick(object sender, RoutedEventArgs e)
{
while(panel.Children.Count>0)
{
Image img = panel.Children[0] as Image;
img.Source = null;
panel.Children.Remove(img);
}
}
I believe my control unloading task is going bad but I don't know how to do it in the right way. Thank you for your help.