WPF-ListBox(图像)的内存使用情况? [英] WPF - ListBox (Image) Memory Usage?

查看:232
本文介绍了WPF-ListBox(图像)的内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我对WPF ListBox消耗大量内存感到困扰...

问题出在我在列表框中显示图像时,包含所有图像的外部文件夹只有30MB,但是当填充列表框时,应用程序的内存使用量(根据任务管理器)从〜50MB跃升到450MB!

问题
为什么将图像加载到应用程序后会消耗大量内存?
有没有一种方法可以更有效地加载图像?

代码

Hello All,
I am having a spot of bother with a WPF ListBox consuming large amounts of memory...

The problem comes when I display Images in a ListBox, the external folder which contains all the images is only 30MB but when the ListBox get populated the applications memory usage (according to Task Manager) jumps from ~50MB to 450MB!

The Questions
Why do Images consume so much more memory when loaded into the Application?
Is there a way to load Images more efficiently?

The Code

public void LoadMusicCollection()
        {
            var query = from s in MusicDC.music_ALBUMs
                        orderby s.album_NAME
                        select s;

            MusicTilesListBox.ItemsSource = query;
        }


用于在每一行中显示图像(和一些文本)的DataTemplate ...


The DataTemplate to display the Image (and some text) in each row...

<DataTemplate x:Key="AlbumDataTemplate">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,10,10,10">
            <Image Height="177" Width="177" Stretch="Fill" VerticalAlignment="Center" Source="{Binding album_ARTWORK}" />
            <Label Content="{Binding album_NAME}" FontWeight="Bold" FontFamily="Century Gothic" Foreground="White" MaxWidth="177" HorizontalAlignment="Center"/>
            <Label Content="{Binding album_ARTIST}" Padding="0,0,0,0" FontFamily="Century Gothic" Foreground="White" MaxWidth="177" HorizontalAlignment="Center"/>
        </StackPanel>
    </DataTemplate>



谢谢大家的事先帮助,这让我呆了一阵子了...

亲切的问候,
Alex



Thank you all for your help in advance, this has had me stuck for a while now...

Kind Regards,
Alex

推荐答案

如果磁盘上的图像为压缩格式(大多数图像文件格式使用某种形式的压缩),则内存占用空间将更大. br/> 如果图像的大小为100x100像素,并使用8bits作为颜色深度,则该图像的原始数据将占用100x100 = 10,000字节,这就是必须渲染到屏幕上的数据量.

例如,如果您创建一个4000x4000 24位全白图像(因为对于jpeg来说很容易压缩),并将其另存为jpeg,则它将占用磁盘上约250k(取决于压缩级别),但是如果您将该图像加载到BitmapSource中;

If the image on disk is in compressed format (and most image file formats use some form of compression), the in-memory footprint will be larger.
If the image is 100x100 pixels in size and uses 8bits for colour depth, the raw data for that image would take up 100x100=10,000 bytes and that''s the amount of data that has to be rendered to the screen.

For example, if you create a 4000x4000 24bit completely white image (because that''s easy to compress for jpeg), and save that as a jpeg, it will take up some 250k on disk (depending on compression level), but if you load that image into a BitmapSource;

src.BeginInit();
src.UriSource = new Uri("C:\\temp\\large.jpg", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();



然后,应用程序的内存占用量将立即跳升约55meg,因为未压缩的图像版本存储在内存中.

/Fredrik



Then the memory foot print of your application will immediately jump up by something like 55meg, because the uncompressed version of the image is stored in memory.

/Fredrik


大家好,
我找到了一些有关在解压缩之前减小Image大小的文档(感谢Fredrik)并将其加载到缓存中.

由Microsoft.com提供
Hello All,
I found some documentation on redcuing the size of the Image before uncompressing (thanks Fredrik) and loading it into the cache.

courtesy of Microsoft.com
<!-- Simple image rendering. However, rendering an image this way may not
     result in the best use of application memory. See markup below which
     creates the same end result but using less memory. -->
<image width="200">
Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"/>

<image width="200">
  <image.source>
    <!-- To save significant application memory, set the DecodePixelWidth or  
     DecodePixelHeight of the BitmapImage value of the image source to the desired 
     height and width of the rendered image. If you don't do this, the application will 
     cache the image as though it were rendered as its normal size rather then just 
     the size that is displayed. -->
    <!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth
         or DecodePixelHeight but not both. -->
    <bitmapimage decodepixelwidth="200"

     UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg" />
  </bitmapimage></image.source>
</image></image>



问候,
亚历克斯



Regards,
Alex


这篇关于WPF-ListBox(图像)的内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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