Problem opening JPEG from Isolated Storage on Wind

2019-06-24 00:27发布

问题:

Scenario

  1. App opens
  2. Checks to see if image for background exists in Isolated Storage
  3. If not, downloads from web, and saves it to Isolated Storage
  4. Loads the image from Isolated Storage and sets it as Background on a Panorama-control

Problem

The image is not loaded in GUI.. When I'm inspecting the byte-array received from isolated storage, it contains the same amount of bytes as was written initially, but the image doesn't show up.

Here's some test-code I'm currently using to try and figure out the problem:

using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!appStorage.FileExists(@"default.jpg"))
                {
                    BitmapImage bmp = sender as BitmapImage;
                    byte[] bytes = bmp.ConvertToBytes();
                    using (var inputfile = appStorage.CreateFile(@"default.jpg"))
                    {
                        inputfile.Write(bytes, 0, bytes.Length);
                    }
                }
                using (var isfs = appStorage.OpenFile(@"default.jpg", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(isfs);
                    MainPanorama.Background = new ImageBrush { Opacity = 0.4, Stretch = Stretch.None, ImageSource = bmp };
                }
            }

Where sender is a loaded image from some other source I've tried setting the sender as backgroundimage on the MainPanorama-control, and that works just fine. So the problem is in the loading from Isolated Storage.

Any ideas?

回答1:

Edit: Sounds like this has to be an issue with timing or with random access to the stream.

Things you could try:

  1. Try loading the entire image into an in memory array - a MemoryStream - and then use that in the SetSource call

  2. Try removing the unused code - the .ImageOpened delegate and the img = new Image() call

  3. if those things don't help then try checking the two streams at the byte level.

For more info on 1, see How Do I Load an Image from Isolated Storage and Display it on the Device? - note that this is Microsoft's support official sample and it loads the image into an in memory MemoryStream before using it in the on-screen Image.

Microsoft's code:

// The image will be read from isolated storage into the following byte array
        byte [] data;

        // Read the entire image in one go into a byte array
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            // Open the file - error handling omitted for brevity
                                 // Note: If the image does not exist in isolated storage the following exception will be generated:
            // System.IO.IsolatedStorage.IsolatedStorageException was unhandled
            // Message=Operation not permitted on IsolatedStorageFileStream
            using (IsolatedStorageFileStream isfs = isf.OpenFile("WP7Logo.png", FileMode.Open, FileAccess.Read))
            {
                // Allocate an array large enough for the entire file
                data = new byte[isfs.Length];

                // Read the entire file and then close it
                isfs.Read(data, 0, data.Length);
                isfs.Close();
            }
        }

        // Create memory stream and bitmap
        MemoryStream ms = new MemoryStream(data);
        BitmapImage bi = new BitmapImage();

        // Set bitmap source to memory stream
        bi.SetSource(ms);

        // Create an image UI element – Note: this could be declared in the XAML instead
        Image image = new Image();

        // Set size of image to bitmap size for this demonstration
        image.Height = bi.PixelHeight;
        image.Width = bi.PixelWidth;

        // Assign the bitmap image to the image’s source
        image.Source = bi;

        // Add the image to the grid in order to display the bit map
        ContentPanel.Children.Add(image);

Please do report back on what fixed it.



回答2:

My guess is that it is an issue of timing. Is this code getting called before the UI is ready to display images? If the visual tree isn't fully loaded I'm not sure what happens when you set the image source.

Try something like this psuedo code:

MyPage() { this.Loaded += () => YourImageLoadMethod; InitializeComponent(); }