1

I can load JPEGs fine using the specialised JPEG load jpeg method, I can also save PNGs fine using the many methods detailed on SO.

However whenever I create a stream for loading a PNG from isolated storage it results in a BitmapImage of zero size.

Here is what I have ...

public static async Task<BitmapImage> ReadBitmapImageFromIsolatedStorage(string fn)
{

        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

        if (local != null)
        {
            Stream file = await local.OpenStreamForReadAsync(fn);
            BitmapImage bi = new BitmapImage();
            bi.SetSource(file);
            return bi;

        }

        return null;

}

I've tried many variations as well. It seems there is some kind of delay to reading the stream when creating BitmapImages which means often the stream is disposed of before the BitmapImage reads it. In WPF there is an option that can be set but WIndows Phone BitmapImage does not have this option.

Brendan
  • 18,771
  • 17
  • 83
  • 114
  • Thought I've not tired it - but maybe http://stackoverflow.com/questions/4817874/problem-opening-jpeg-from-isolated-storage-on-windows-phone-7 will help you. In the answer @Stuart is opening a PNG file from IS. – Romasz Jan 29 '14 at 08:40

2 Answers2

0

Try this:

BitmapImage image = new BitmapImage();
image.DecodePixelWidth = 500; //desired width, optional
image.DecodePixelHeight = 500; //desired height, optional

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (myIsolatedStorage.FileExists("imagepath"))
    {
        using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("imagepath", FileMode.Open, FileAccess.Read))
        {
            image.SetSource(fileStream);
        }
    }
}
philorube
  • 155
  • 8
  • Unless the `DecodePixel[Width|Height]` makes a difference, it does not work. Only in the last few attempts did I change to the new `Windows.Storage` method ... – Brendan Jan 28 '14 at 22:33
0

This is what I found works in the end using the new Windows.Storage APIs

I'm not 100% clear what the problem was originally but this works. This has some extra functionality in that if it fails to read, it retries a second or so later ...

public static async Task<BitmapImage> ReadBitmapImageFromIsolatedStorage(string fn)
    {
        Uri uri = new Uri(String.Format("ms-appdata:///local/{0}", fn));
        // FileAccessAttempts is just an int that determines how many time 
        // to try reading before giving up
        for (int i = 0; i < AppConfig.FileAccessAttempts; i++)
        {
            try
            {
                StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
                Stream stream = await file.OpenStreamForReadAsync();
                BitmapImage bi = new BitmapImage();
                bi.SetSource(stream);
                return bi;
            }
            catch
            {
                // Similar functions also have a ~1 second retry interval so introduce
                // a random element to prevent blocking from accidentally synched retries
                Random r = new Random();
                System.Threading.Thread.Sleep(950 + (int)Math.Floor(r.Next() * 50.0));
            }
        }
        return new BitmapImage();
    }
Brendan
  • 18,771
  • 17
  • 83
  • 114