0

我已将近 150 张高分辨率图像存储到独立存储中,分辨率为 789x 1299。我的问题是,当我将 60 - 70 个图像加载到列表集合中时,它工作正常,但是当 bi.SetSource(ms) 中发生超过 70 个内存不足异常时。我在项目模板中使用虚拟化 satck 面板。什么原因

        List<SampleData> data = new List<SampleData>();
        try
        {

            for (int i = 0; i < 150; i++)
            {

                byte[] data2;

                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {

                    using (IsolatedStorageFileStream isfs = isf.OpenFile("IMAGES" + i + ".jpg", FileMode.Open, FileAccess.Read))
                    {
                        data2 = new byte[isfs.Length];
                        isfs.Read(data2, 0, data2.Length);
                        isfs.Close();
                    }

                }


                MemoryStream ms = new MemoryStream(data2);

                BitmapImage bi = new BitmapImage();

                bi.SetSource(ms);

                data.Add(new SampleData() { Name = bi });



            }
            this.list.ItemsSource = data;

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());

        }



        }



    public class SampleData
    {
        public ImageSource Name
        {
            get;
            set;
        }


    }
}

}

      <ListBox x:Name="list" Width="480">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="10">

                        <Image Height="500" Width="500" Source="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" Orientation="Vertical"/>

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
4

1 回答 1

1

我相信通过创建第BitmapImages一个,WP7 会预先为每个图像分配内存,而不是当列表框中的项目滚动到视图中时。尝试将字节数组而不是BitmapImage, 存储在SampleData类中,然后BitmapImage在调用时通过属性创建 。

所以 SampleData 看起来像这样:

public class SampleData
{
    public byte[] ImgData {get;set;}
    public BitmapImage Image
    {
        get
        {
            BitmapImage bi = new BitmapImage();
            MemoryStream ms = new MemoryStream(ImgData);
            bi.SetSource(ms);
            return bi;
        }
    }
}

由于您有许多高分辨率图像,您可能仍然会遇到性能问题 - 我可能建议为 存储这些图像的较低分辨率版本ListBox,然后在用户需要查看该特定图像时显示高分辨率图像?希望这可以帮助!

于 2012-06-01T14:47:11.747 回答