如何在应用程序的设置中保存图像或文件流? UWP C#

问题描述

我有一个包含一些 ListViewItem 的列表视图,每个都包含一个文本框和一个图像。

用户可以添加删除这些 ListViewItem,所以我想在每次关闭应用时将它们保存在设置中,并在每次打开时加载。

我设法在设置中保存文本框的文本并加载它们,但是如何保存和加载图像/图像源/文件流?

用户fileopenpicker 中选择一个图像时创建图像,然后使用文件流创建一个位图图像,作为 ListViewItem 的图像源。

private async void openFileInputEvent(object sender,RoutedEventArgs e)
    {
        var picker = new Windows.Storage.Pickers.fileopenpicker();
        picker.viewmode = Windows.Storage.Pickers.Pickerviewmode.Thumbnail;
        picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".jpeg");
        picker.FileTypeFilter.Add(".png");

        Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
        if (file != null)
        {
            using (IRandomAccessstream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapImage tmpimg = new BitmapImage();
                await tmpimg.SetSourceAsync(fileStream);
                imglist.Add(tmpimg);
            }
        }
        else
        {
            var dialog = new MessageDialog("File not chosen","No chosen Image");
            await dialog.ShowAsync();
            imglist.Add(new BitmapImage());
        }
    }

所有 ListViewItems 图像的位图图像都存储在位图图像列表 (List<BitmapImage>) 中,并通过选择列表中的最后一项 (imglist[imglist.Count-1]) 来选择它们

我想保存和加载这个图像/他们的源/文件流到他们的源,有什么办法吗?

我可以保存图像的路径,但我以后不能以路径为源创建图像,它必须是一个文件流。

解决方法

如何在应用程序的设置中保存图像或文件流? UWP C#

LocalSettings 不支持存储大数据,恐怕你不能直接把 BitmapImage 存储到 LocalSettings 中,对于你的场景,我们建议你把你的图片存储在应用程序的 {{ 3}} 并使用 LocalFolder 访问它们。