2012-06-01 28 views
0

我将近150个高分辨率图像存储到具有789x 1299分辨率的独立存储中。我的问题是,当我加载60 - 70图像列表集合它工作正常,但当它超过70内存异常发生在bi.SetSource(毫秒)。我正在使用项目模板中的虚拟化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> 

回答

1

相信通过创建BitmapImages第一,WP7对每个图像前面分配存储器,而不是当在列表框中滚动到视图中的项目。尝试在SampleData类中存储字节数组,而不是BitmapImage,然后在通过属性调用时创建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,然后在用户需要查看该特定图像时显示高分辨率图像?希望这可以帮助!