最小化图像的内存消耗列表框(WPF) [英] Minimizing memory consumption listbox of images (WPF)

查看:64
本文介绍了最小化图像的内存消耗列表框(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,它绑定到ObservableCollection并采用文件名来显示图像

I have listbox which bind to ObservableCollection and take filename to display images

我的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 ?

推荐答案

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

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

您还可以加载缩略图,而不是完整的照片.

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

一些需要阅读的资源:

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

这篇关于最小化图像的内存消耗列表框(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆