C# WinRT C-内存不足-读取图像文件

C# WinRT C-内存不足-读取图像文件,c#,windows-runtime,bitmapimage,C#,Windows Runtime,Bitmapimage,我有一个相当简单的程序,尝试读取图片库中的所有图形文件。我为每个文件创建一个位图图像,但我从不使用或引用它。在读取了61个文件后,我得到了一个内存不足的异常-它是可复制的,它总是在第61个文件上消失 BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); Windows.Storage.Streams.IRandomAccessStream fileStream = await pic.Op

我有一个相当简单的程序,尝试读取图片库中的所有图形文件。我为每个文件创建一个位图图像,但我从不使用或引用它。在读取了61个文件后,我得到了一个内存不足的异常-它是可复制的,它总是在第61个文件上消失

BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
Windows.Storage.Streams.IRandomAccessStream fileStream =
      await pic.OpenAsync(Windows.Storage.FileAccessMode.Read);
      await bitmapImage.SetSourceAsync(fileStream);
上面的代码片段显示了我所做工作的要点。我在一个方法中创建了一个bitmapImage,将其返回给调用者,并且从不保留或引用它

既然我没有保留对位图图像的引用,GC不应该收集未引用的位图图像吗

任何帮助或指点都将不胜感激

谢谢

坦率的

---这是扩展代码---

    private IEnumerator<StorageFile> hdEnum = null;

    /*
     * Initialize to read all the pic files in the PicturesLibary.
     * 
     */

    private async Task initializeEnum()
    {
        if (hdEnum != null)
        {
            // I have already been initialised so go away
            return;
        }

        folder = Windows.Storage.KnownFolders.PicturesLibrary;

        // Set query options with filter and sort order for results
        List<string> fileTypeFilter = new List<string>();
        fileTypeFilter.Add(".jpg");
        fileTypeFilter.Add(".png");
        fileTypeFilter.Add(".bmp");
        fileTypeFilter.Add(".gif");
        var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);

        // Create query and retrieve files
        var query = folder.CreateFileQueryWithOptions(queryOptions);

        // Query the folder.
        var fileList = await query.GetFilesAsync();
        hdEnum = fileList.GetEnumerator();
    }

    /*
     * Read and return the next picture file in the PicturesLibrary
     */

    public async Task<BitmapImage> next()
    {
        BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); ;

        StorageFile pic = null;

        if (hdEnum == null)
        {
            await initializeEnum();
        }

        if (hdEnum.MoveNext())
        {
            pic = hdEnum.Current;   // hdEnum is an enumerator on the list of image files
        }           

        if (pic != null)
        {
            // come here if I have an image to read
            String path = pic.Path;


            Windows.Storage.Streams.IRandomAccessStream fileStream =
            await pic.OpenAsync(Windows.Storage.FileAccessMode.Read);
            await bitmapImage.SetSourceAsync(fileStream);
            fileStream.Dispose();

            // force a GC - this makes no difference, it still dies after the 61st file
            GC.Collect();
            Debug.WriteLine("Read: " + ++fileCount + " " + pic.Path + " " + GC.GetTotalMemory(true));
        }

         bitmapImage = null;  // EXPLICITLY MAKING IT NULL - no references
        return (bitmapImage);
    }

获取irandomaccesstream对象后,如何使用该对象?你能显示相关的代码吗?我把相关的代码位添加到了我的原始消息中。如图所示,我处理了文件流并返回了一个位图图像,但调用者从未引用过它;然后一切顺利。