2
votes

I am saving image into isolated storage each image have a different imageFileName. But i am having problem to retrieve all the saved image in a listbox. Only managed to retrieve the latest image saved. When i hard code the filepath then can retrieve it. I hope anyoen can help me with the code.. Hopefully anyone can try editing my code. Thanks.

Save code:

private void SaveToLocalStorage(string imageFolder, string imageFileName)
        {
            imageFileName = App.imagePath;

            var isf = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isf.DirectoryExists(imageFolder))
            {
                isf.CreateDirectory(imageFolder);
            }

            string filePath = Path.Combine(imageFolder, imageFileName);
            using (var stream = isf.CreateFile(filePath))
            {
                var bmp = new WriteableBitmap(inkCanvas, inkCanvas.RenderTransform);
                bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            }
            MessageBox.Show(filePath            }

Retrieve code:

private void LoadFromLocalStorage(string imageFolder, string imageFileName )
        {
            var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isoFile.DirectoryExists(imageFolder))
            {
                isoFile.CreateDirectory(imageFolder);
            }

            string filePath = Path.Combine(imageFolder, imageFileName);
            using (var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
            {
                var imageSource = PictureDecoder.DecodeJpeg(imageStream);

                BitmapImage bi = new BitmapImage();

                ListBoxItem item = new ListBoxItem();
                bi.SetSource(imageStream);
                item.Content = new Image() { Source = bi, MaxHeight = 100, MaxWidth = 100 };
                listBox1.Items.Add(item);
            }
}
2

2 Answers

4
votes

Try something like:

private void LoadFromLocalStorage(string imageFolder)
{
    var isoFile = IsolatedStorageFile.GetUserStoreForApplication();

    // Check if directory exists
    if(!isoFile.DirectoryExists(imageFolder))
    {
        throw new Exception("Image directory not found");
    }

    // Clear listbox
    listBox1.Items.Clear();

    // Get files
    foreach(string fileName in isoFile.GetFileNames())
    {
        string filePath = Path.Combine(imageFolder, fileName);
        using(var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
        {
            var imageSource = PictureDecoder.DecodeJpeg(imageStream);

            BitmapImage bi = new BitmapImage();

            ListBoxItem item = new ListBoxItem();
            bi.SetSource(imageStream);
            item.Content = new Image() { Source = bi, MaxHeight = 100, MaxWidth = 100 };
            listBox1.Items.Add(item);
        }
    }
}
0
votes

Hard to say without seeing more of your code, but it looks like you're only retrieving one file. Elsewhere in your code are you getting a list of all of the files that should be in the IsolateStorage directory and looping through them? Are you seeing any error messages, or is it just failing silently?