Problem opening JPEG from isolated storage on Windows Phone 7

Scenario

  • Open application
  • Checks if image for background exists in isolated storage
  • If not, download from the Internet and save it in isolated storage
  • Loads an image from isolated storage and sets it as a background in panorama control mode

Problem

The image does not load into the graphical interface. When I check the byte array received from the isolated storage, it contains the same number of bytes as at the beginning, but the image is not displayed.

Here is some test code that I am currently using to try to 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 the downloaded image from another source. I tried to configure the sender as backgroundimage on MainPanorama-control, and this works fine. Therefore, the problem is loading from isolated storage.

Any ideas?

+4
source share
2 answers

Edit: It looks like this should be a problem with synchronization or with random access to the stream.

What could you try:

  • Try loading the entire image into a memory array - MemoryStream, and then use it in a SetSource call

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

  • If these things do not help, try checking the two streams at the byte level.

For more information on 1 cm. How do I download an image from isolated storage and display it on my device? - Please note that this Microsoft supports the official sample and loads the image into MemoryStream before using it in the screen image.

Microsoft 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 report what has been fixed.

+2
source

I guess this is a time issue. Is this code called before the user interface is ready to display images? If the visual tree is not fully loaded, I'm not sure what will happen when you set the image source.

Try something like this psuedo code:

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

0
source

Source: https://habr.com/ru/post/1337332/


All Articles