Scenario
- App opens
- Checks to see if image for background exists in Isolated Storage
- If not, downloads from web, and saves it to Isolated Storage
- 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?
Edit: Sounds like this has to be an issue with timing or with random access to the stream.
Things you could try:
Try loading the entire image into an in memory array – a MemoryStream – and then use that in the SetSource call
Try removing the unused code – the .ImageOpened delegate and the img = new Image() call
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:
Please do report back on what fixed it.