Search code examples
c#imagewindows-phone-7writeablebitmapisolatedstoragefile

Null Reference Pointer error at WriteableBitmap when trying to save image in WP7


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.


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.