WinRT GridView崩溃了

时间:2014-03-19 19:00:07

标签: c# xaml gridview windows-runtime

当显示超过13 (12或更少工作正常)大图像(3264x2448)缩小为320x180图像项时 - GridView崩溃并出现非托管异常:0x7583A862处未处理的异常(combase.dll)。

的Xaml:

<GridView x:Name="ImagesGridView" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Image Stretch="Fill" Source="{Binding Source}" Height="180" Width="320" />
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
</GridView>

正在加载代码:

private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        var bitmapImage = new BitmapImage();
        var stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        stream.CloneStream();
        return bitmapImage;

    }

    public async void GetPictures()
    {
        var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, new[] { ".png", ".jpg" });
        var picturesQuery = await KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions).GetFilesAsync();
        foreach (var pictureFile in picturesQuery)
        {
            var bitmap = await LoadImage(pictureFile);
            var img = new Image { Source = bitmap };
            ImagesGridView.Items.Add(img);
        }
    }

崩溃不是在加载图片时发生的,但是在所有加载成功后,应用程序应该在GridView中显示图像。此外,我正在使用相同图像的副本,以确保所有图像都相同。

对我来说,在应用程序中加载大图像似乎很常见,所以也许我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

虽然您的图像控件仅为320x180,但您正在以全分辨率加载图像,因此您尝试将3264x2448x4(每像素字节数)x13 = ~400MB的纹理输入到视频卡中,这可能有点紧张。

尝试使用StorageFile.GetThumbnailAsync()加载图片,例如

private static async Task<BitmapImage> LoadImageAsync(StorageFile pictureFile)
{
    using (var stream = await pictureFile.GetThumbnailAsync(
        ThumbnailMode.SingleItem,
        320,
        ThumbnailOptions.ResizeThumbnail))
    {
        var bitmap = new BitmapImage();
        await bitmap.SetSourceAsync(stream);
        return bitmap;
    }
}