最小化图像的内存消耗列表框 (WPF)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14525859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:14:09  来源:igfitidea点击:

Minimizing memory consumption listbox of images (WPF)

c#wpflistbox

提问by Irwan

I have listbox which bind to ObservableCollection and take filename to display images enter image description here

我有绑定到 ObservableCollection 并取文件名来显示图像的列表框 在此处输入图片说明

My xaml is:

我的 xaml 是:

<Window x:Class="ThumbnailsView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="578" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="55"/>
        </Grid.RowDefinitions>

            <ListBox Grid.Row="0" x:Name="ImageListbox"
        ItemsSource="{Binding}" 
        Background="AliceBlue" ScrollViewer.HorizontalScrollBarVisibility="Disabled">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Height="16" VerticalAlignment="Top" Margin="0,10,0,0"/>
                            <Image Margin="10,10,10,0" Height="64" Width="64" VerticalAlignment="Top">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="64" UriSource="{Binding Path=Name}"/>
                                </Image.Source>                            
                            </Image>
                    </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

        <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

            <Button Grid.Row="1" Content="Get Images" Name="getImageBtn" Click="getImageBtn_Click" Width="100" Height="30"></Button>

    </Grid>
</Window>

The problem is, it loads entire images and will consume a lot of ram if I have a large collection. How to minimize its memory consumption ?

问题是,它会加载整个图像,如果我有大量收藏,则会消耗大量内存。如何最小化它的内存消耗?

采纳答案by dutzu

Enable UI Virtualization. Then the UI controls will be recycled and the minimal amount of memory will be used.

启用 UI 虚拟化。然后 UI 控件将被回收并使用最少的内存。

You could also load thumbmnails instead of the fully fledged photos.

您还可以加载缩略图而不是完全成熟的照片。



Some resources to read on:

一些可供阅读的资源:

http://www.codeproject.com/Articles/34405/WPF-Data-Virtualizationhttps://stackoverflow.com/questions/14456075/how-to-enable-ui-virtualization-in-standard-wpf-listviewWPF ListBox with a ListBox - UI Virtualization and Scrollinghttp://www.zagstudio.com/blog/497#.UQKxpScqb6U

http://www.codeproject.com/Articles/34405/WPF-Data-Virtualization https://stackoverflow.com/questions/14456075/how-to-enable-ui-virtualization-in-standard-wpf-listview WPF ListBox使用 ListBox - UI 虚拟化和滚动http://www.zagstudio.com/blog/497#.UQKxpScqb6U

回答by Coincoin

Once the image is loaded, resize it to a more managable size then release the unused big image. This will still take a long time to load but it will take less memory. To reduce loading time see dutzu'sanswer and use lazy loading and virtualization.

加载图像后,将其调整为更易于管理的大小,然后释放未使用的大图像。这仍然需要很长时间才能加载,但会占用更少的内存。要减少加载时间,请参阅dutzu's答案并使用延迟加载和虚拟化。

回答by TYY

Use a Virtualizing Stackpanel Look at the link for an example as to how to do it.

使用虚拟化堆栈面板查看链接以获取有关如何执行此操作的示例。

http://www.jonathanantoine.com/2011/10/07/wpf-4-5-%E2%80%93-part-11-new-features-for-the-virtualizingpanel/

http://www.jonathanantoine.com/2011/10/07/wpf-4-5-%E2%80%93-part-11-new-features-for-the-virtualizingpanel/