• <tfoot id='Own8f'></tfoot>
      <bdo id='Own8f'></bdo><ul id='Own8f'></ul>
    <i id='Own8f'><tr id='Own8f'><dt id='Own8f'><q id='Own8f'><span id='Own8f'><b id='Own8f'><form id='Own8f'><ins id='Own8f'></ins><ul id='Own8f'></ul><sub id='Own8f'></sub></form><legend id='Own8f'></legend><bdo id='Own8f'><pre id='Own8f'><center id='Own8f'></center></pre></bdo></b><th id='Own8f'></th></span></q></dt></tr></i><div id='Own8f'><tfoot id='Own8f'></tfoot><dl id='Own8f'><fieldset id='Own8f'></fieldset></dl></div>

    <small id='Own8f'></small><noframes id='Own8f'>

        <legend id='Own8f'><style id='Own8f'><dir id='Own8f'><q id='Own8f'></q></dir></style></legend>

        当我的 ListBox 中有图像时,为什么会出现 OutOfMemoryException?

        Why do I get an OutOfMemoryException when I have images in my ListBox?(当我的 ListBox 中有图像时,为什么会出现 OutOfMemoryException?)

      1. <i id='HS4Cq'><tr id='HS4Cq'><dt id='HS4Cq'><q id='HS4Cq'><span id='HS4Cq'><b id='HS4Cq'><form id='HS4Cq'><ins id='HS4Cq'></ins><ul id='HS4Cq'></ul><sub id='HS4Cq'></sub></form><legend id='HS4Cq'></legend><bdo id='HS4Cq'><pre id='HS4Cq'><center id='HS4Cq'></center></pre></bdo></b><th id='HS4Cq'></th></span></q></dt></tr></i><div id='HS4Cq'><tfoot id='HS4Cq'></tfoot><dl id='HS4Cq'><fieldset id='HS4Cq'></fieldset></dl></div>
          <tbody id='HS4Cq'></tbody>

        <small id='HS4Cq'></small><noframes id='HS4Cq'>

      2. <legend id='HS4Cq'><style id='HS4Cq'><dir id='HS4Cq'><q id='HS4Cq'></q></dir></style></legend>

            <tfoot id='HS4Cq'></tfoot>
              <bdo id='HS4Cq'></bdo><ul id='HS4Cq'></ul>

                1. 本文介绍了当我的 ListBox 中有图像时,为什么会出现 OutOfMemoryException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在我的自定义库中显示存储在 Windows Phone 8 照片文件夹中的所有图像,该库使用 ListBox 来显示图像.

                  I want to display all images stored in the Windows Phone 8 photo folder in my custom gallery which uses a ListBox for displaying the images.

                  ListBox代码如下:

                      <phone:PhoneApplicationPage.Resources>
                          <MyApp:PreviewPictureConverter x:Key="PreviewPictureConverter" />
                      </phone:PhoneApplicationPage.Resources>
                  
                      <ListBox Name="previewImageListbox" VirtualizingStackPanel.VirtualizationMode="Recycling">
                          <ListBox.ItemsPanel>
                              <ItemsPanelTemplate>
                                  <VirtualizingStackPanel CleanUpVirtualizedItemEvent="VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1">
                                  </VirtualizingStackPanel>
                              </ItemsPanelTemplate>
                          </ListBox.ItemsPanel>
                          <ListBox.ItemTemplate>
                              <DataTemplate>
                                  <Grid>
                                      <Image Source="{Binding Converter={StaticResource PreviewPictureConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                  </Grid>
                              </DataTemplate>
                          </ListBox.ItemTemplate>
                       </ListBox>
                  

                  使用以下转换器:

                  public class PreviewPictureConverter : System.Windows.Data.IValueConverter
                  {
                      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                      {
                          PreviewImageItem c = value as PreviewImageItem;
                          if (c == null)
                              return null;
                          return c.ImageData;
                      }
                  
                      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  图像存储在自定义类中:

                  Images are stored in a custom class:

                  class PreviewImageItem
                  {
                      public Picture _picture = null;
                      public BitmapImage _bitmap = null;
                  
                      public PreviewImageItem(Picture pic)
                      {
                          _picture = pic;
                      }
                  
                      public BitmapImage ImageData 
                      {
                          get
                          {
                              System.Diagnostics.Debug.WriteLine("Get picture " + _picture.ToString());
                              _bitmap = new BitmapImage();
                              Stream data = _picture.GetImage();
                              try
                              {
                                  _bitmap.SetSource(data); // Out-of memory exception (see text)
                              }
                              catch (Exception ex)
                              {
                                  System.Diagnostics.Debug.WriteLine("Exception : " + ex.ToString());
                              }
                              finally
                              {
                                  data.Close();
                                  data.Dispose();
                                  data = null;
                              }
                  
                              return _bitmap;
                          }
                      }
                  }
                  

                  以下代码用于设置ListBox数据源:

                  The following code is used to set the ListBox data source:

                  private List<PreviewImageItem> _galleryImages = new List<PreviewImageItem>();
                  
                  using (MediaLibrary library = new MediaLibrary())
                  {
                      PictureCollection galleryPics = library.Pictures;
                      foreach (Picture pic in galleryPics)
                      {
                          _galleryImages.Add(new PreviewImageItem(pic));
                      }
                  
                      previewImageListbox.ItemsSource = _galleryImages;
                  };
                  

                  最后是清理"代码:

                  private void VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1(object sender, CleanUpVirtualizedItemEventArgs e)
                  {
                      PreviewImageItem item = e.Value as PreviewImageItem;
                  
                      if (item != null)
                      {
                          System.Diagnostics.Debug.WriteLine("Cleanup");
                          item._bitmap = null;
                      }
                  }
                  

                  所有这些都可以正常工作,但是在几张图片之后代码会因 OutOfMemoryException 而崩溃(尤其是在快速滚动时).VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1 方法在 ListBox 滚动时被定期调用(例如,每 2 或 3 个列表框条目).

                  All this works fine but the code crashes with an OutOfMemoryException after a few images (especially when scrolling fast). The method VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1 is called regulary (e.g. every 2 or 3 listbox entries) when the ListBox is scrolled.

                  这个示例代码有什么问题?

                  What's wrong with this sample code?

                  为什么没有释放内存(足够快)?

                  Why is memory not freed (fast enough)?

                  推荐答案

                  哦,我最近花了一整天的时间来完成这个工作!

                  Oh, I recently killed whole day to make this working!

                  所以解决办法是:

                  使您的图像控件免费资源.所以设置

                  Make your Image control free resources. So set the

                  BitmapImage bitmapImage = image.Source as BitmapImage;
                  bitmapImage.UriSource = null;
                  image.Source = null;
                  

                  如前所述.

                  确保在列表的每个项目上虚拟化 _bitmap.您应该按需加载它(LongListSelector.Realized 方法)并且您必须销毁它!它不会自动收集,而且 GC.Collect 也不起作用.空引用也不起作用:(但这里是方法:制作 1x1 像素文件.将其复制到程序集中并从中生成资源流,以使用 1x1 像素空白处理您的图像.将自定义 dispose 方法绑定到 LongListSelector.UnRealized 事件(e.Container 处理您的列表项).

                  Make sure you virtualize _bitmap on every item of the list. You should load it on demand (LongListSelector.Realized method) and you have to destroy it! It won't going to collect automatically and GC.Collect doesn't work either. Null reference is not working too :( But here is the method: Make 1x1 pixel file. Copy it into assembly and make resource stream from it to dispose your images with 1x1 pixel blank. Bind custom dispose method to LongListSelector.UnRealized event (e.Container handles your list item).

                  public static void DisposeImage(BitmapImage image)
                  {
                      Uri uri= new Uri("oneXone.png", UriKind.Relative);
                      StreamResourceInfo sr=Application.GetResourceStream(uri);
                      try
                      {
                          using (Stream stream=sr.Stream)
                          {
                              image.DecodePixelWidth=1; //This is essential!
                              image.SetSource(stream);
                          }
                      }
                      catch { }
                  }
                  

                  在 LongListSelector 中为我工作,有 1000 张图像,每张 400 宽.

                  Working for me in LongListSelector with 1000 images 400 width each.

                  如果您错过了数据收集的第 2 步,您可以看到良好的结果,但在滚动 100-200 个项目后内存溢出.

                  If you miss the 2 step with the data collection you can see the the good results but the memory overflows after 100-200 items scrolled.

                  这篇关于当我的 ListBox 中有图像时,为什么会出现 OutOfMemoryException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                  • <legend id='bb29U'><style id='bb29U'><dir id='bb29U'><q id='bb29U'></q></dir></style></legend>
                      • <bdo id='bb29U'></bdo><ul id='bb29U'></ul>

                      • <tfoot id='bb29U'></tfoot>

                            <tbody id='bb29U'></tbody>

                          <small id='bb29U'></small><noframes id='bb29U'>

                        1. <i id='bb29U'><tr id='bb29U'><dt id='bb29U'><q id='bb29U'><span id='bb29U'><b id='bb29U'><form id='bb29U'><ins id='bb29U'></ins><ul id='bb29U'></ul><sub id='bb29U'></sub></form><legend id='bb29U'></legend><bdo id='bb29U'><pre id='bb29U'><center id='bb29U'></center></pre></bdo></b><th id='bb29U'></th></span></q></dt></tr></i><div id='bb29U'><tfoot id='bb29U'></tfoot><dl id='bb29U'><fieldset id='bb29U'></fieldset></dl></div>