How to save an Image or File Stream in application's settings? UWP C#

Viewed 208

I have a List View with some ListViewItems and each one contains a TextBox and an Image.

The user can add or remove these ListViewItems, so I would like to save them in the settings each time the app is closed and load each time it is opened.

I managed to save the TextBoxes' texts in the settings and load them, but how to save and load Images / Image Sources / File Streams?

The images are created when the user chooses an image in a FileOpenPicker and then a Bitmap Image is created with a File Stream to that chosen Image, as the ListViewItem's image source.

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());
        }
    }

The bitmap images for all the ListViewItems' images are stored in a list of bitmap images (List<BitmapImage>) and they are chosen by choosing the last item on the list (imglist[imglist.Count-1])

I would like to save and load this Images / their source / file stream to their source, is there any way to do that?

I could save paths to the images, but I can't later create an Image with the path as its source, it has to be a file stream.

1 Answers

How to save an Image or File Stream in application's settings? UWP C#

LocalSettings does not support store the big data, I'm afraid you can't store the BitmapImage into LocalSettings directly, for your scenario, we suggest you store your image in the app's LocalFolder and access them with uri scheme.

Related