0

不知道爲什麼,以及兩天嘗試不同的東西時,我無處可去。繼續在WriteableBitmap行獲取NRP。你可以看到我已經嘗試關閉並沖洗(並且一起)流。
任何想法,將不勝感激。嘗試在WP7中保存圖像時,WriteableBitmap上的空引用指針錯誤

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); 
       } 
      } 
     } 

我GOOGLE了,直到我失明,不知道現在該做什麼。 在此先感謝您的所有人。

回答

0

「新的BitmapImage」 行之前的圖像添加處理程序,就像這樣:

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

然後,在ImageOpened事件,保存到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); 
      } 
} 

目前正在嘗試在加載之前保存圖像,因此存在空指針異常。

+0

謝謝philorube。花了一些時間重新組織我的代碼,但這是一個調用,並在最終的文件保存期間添加了一個正斜槓。 – NoJoeGo

+0

很高興能幫到你!我曾經有過類似的問題。 :) – philorube

+0

那麼工作,但我的新代碼是不完全正確的,在循環中執行第二個解析方法來獲取字符串,它不會迭代......必須弄清楚如何執行自定義事件來傳遞字符串這樣的處理程序..在MS的例子真的不好。你有什麼技巧提示? – NoJoeGo

0

將所有嘗試將多個圖像下載到獨立存儲並將文件名保留在WP上的所有人發佈。請注意,它需要一個url路徑和來自首次下載的xml文件的文件名,剝離路徑然後用原始名稱保存該文件。 借用了一些代碼(感謝所有人,尤其是philorube),寫了其他的並詛咒了一大堆到這裏,但它的作品。

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(); 
      } 
     } 
相關問題