Question

Not sure why, and two days of trying different things I'm getting nowhere. Keep getting the NRP at the WriteableBitmap line. You can see I've tried to both close and flush (and both together) the stream.
Any ideas would be appreciated.

 IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
        XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
        XDocument document = XDocument.Load(myIsolatedStorage.OpenFile("Selections.xml", FileMode.Open));
        MyClass enclaves = (MyClass)serializer.Deserialize(document.CreateReader());
        enclavesList.ItemsSource = enclaves.Collection1;

            foreach (XElement xencl in document.Descendants("rest"))
               {

            WebClient downloader = new WebClient();
            String theelement = xencl.Element("couplink").Value;
            String nameElement = xencl.Element("coup").Value;
            String uriring = theelement.ToString();
            Uri uri = new Uri(uriring, UriKind.RelativeOrAbsolute);
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(enclavesDownloaded);
            downloader.DownloadStringAsync(uri);

            Random random = new Random();
            int randomNumber = random.Next(0, 100);

            using (IsolatedStorageFile newIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                String tempJPEG = randomNumber.ToString();

                IsolatedStorageFileStream fileStream = newIsolatedStorage.CreateFile(tempJPEG);
                //fileStream.Close();
                //fileStream.Flush();
                BitmapImage image = new BitmapImage(new Uri("" + uri ));
                image.CreateOptions = BitmapCreateOptions.None;
                WriteableBitmap wb = new WriteableBitmap(image);
                System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
              }
           }
         }

I've googled till I'm blind and not sure what to do now. Thanks in advance all you folks.

Was it helpful?

Solution

Add handlers to the image before the "new BitmapImage" line, like so:

this.Image.ImageOpened += ImageOpened;
this.Image.ImageFailed += ImageFailed;

Then, in the ImageOpened event, save to the WriteableBitmap:

private void ImageOpened(object sender, RoutedEventArgs e)
{
    WriteableBitmap wb = new WriteableBitmap((BitmapImage)sender);

        using (IsolatedStorageFile newIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            String tempJPEG = randomNumber.ToString();

            IsolatedStorageFileStream fileStream = newIsolatedStorage.CreateFile(tempJPEG);
            //fileStream.Close();
            //fileStream.Flush();
            BitmapImage image = new BitmapImage(new Uri("" + uri ));
            image.CreateOptions = BitmapCreateOptions.None;
            System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
          }
}

You are currently attempting to save the image before it has loaded, hence the null pointer exception.

OTHER TIPS

Posting this for all who have tried to download multiple images to isolated storage and keep the names of the file on the WP. Notice that it requires a url path with the file name from the xml file that is first downloaded, strips the path then saves the file with the original name. Borrowed some code (Thanks to all, especially philorube), wrote other and cursed a LOT to get here, but it works.

IsolatedStorageFile myIsolatedStorage =   IsolatedStorageFile.GetUserStoreForApplication();

        XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
        XDocument document =    XDocument.Load(myIsolatedStorage.OpenFile("Selections.xml", FileMode.Open));
        MyClass enclaves = (MyClass)serializer.Deserialize(document.CreateReader());


        foreach (var xe in document.Descendants("couplink"))
        {
            mane = xe.Value.ToString();
            WebClient webClient = new WebClient();
            Uri uri = new Uri(mane, UriKind.RelativeOrAbsolute);
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync((uri),mane);
        }
    }

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        int count;
        Stream stream = e.Result;
        byte[] buffer = new byte[1024];
        String imgName = (string)e.UserState;
        String cleanImgName = System.IO.Path.GetFileName(imgName);
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(cleanImgName, FileMode.Create, isf))
            {
                count = 0;
                while (0 < (count = stream.Read(buffer, 0, buffer.Length)))
                {
                    isfs.Write(buffer, 0, count);
                }
                stream.Close();
                isfs.Close();
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top