2013-04-24 38 views
8

結合圖像這是我使用的結合圖像同樣我使用4邊框顯示我最近的圖像的代碼在XAML的System.OutOfMemoryException在閱讀和獨立存儲

   <Border toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3"> 
         <Grid Height="350" Width="400" Margin="70,0,70,0" x:Name="Container1"> 
          <Grid.Background> 
           <ImageBrush ImageSource="{Binding ImageCollection[0]}" Stretch="Uniform" AlignmentX="Left" AlignmentY="Center"/> 
          </Grid.Background> 
          <i:Interaction.Triggers> 
           <i:EventTrigger EventName="Tap"> 
            <i:InvokeCommandAction Command="{Binding ImageTapCommand}" CommandParameter="CONTAINER0"/> 
           </i:EventTrigger> 
          </i:Interaction.Triggers> 
         </Grid> 
        </Border> 

在我的ViewModel中,我使用下面的方法從獨立存儲讀取圖像。

public Stream GetFileStream(string filename, ImageLocation location) 
    { 
     try 
     { 
      lock (SyncLock) 
      { 
       if (location == ImageLocation.RecentImage) 
       { 
        filename = Constants.IsoRecentImage + @"\" + filename; 
       } 
       using (var iSf = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (!iSf.FileExists(filename)) return null; 
        var fs = iSf.OpenFile(filename, FileMode.Open, FileAccess.Read); 
        return fs; 
       } 
      } 

     } 
     catch (Exception ex) 
     { 
       return null; 
     } 

    } 

和獲取流,我會用這個寫在下面方法四樓的WritableBitmap的UI結合

 private WriteableBitmap BuildImage(Stream imageStream) 
    { 
     using (imageStream) 
     { 
      var image = new BitmapImage(); 
      image.SetSource(imageStream); 
      return new WriteableBitmap(image); 
     } 
    } 

在這種情況下,我的問題是導航,並從我的頁面後兩後 - 三倍。該應用程序崩潰在BuildImage()方法,我使用「image.SetSource(imageStream);」方法。我嘗試了很多選擇,但失敗了。我得到的例外是「System.OutOfMemoryException」

我試過圖像控制,而不是圖像刷。

我試過位圖而不是WritableBitmap等,但結果是一樣的。

如果我使用小圖像,應用程序崩潰率會降低。但通過相機拍攝的圖像的崩潰率很高。

我正在嘗試解決這個問題的最後一個星期,但沒有找到任何替代方案來解決這個問題。

我發現了一個link談到有關類似問題,但沒有得到很多在默認情況下解決這一問題

回答

3
Try this, 

    var bitmapImage = new BitmapImage(); 
    bitmapImage.SetSource(stream); 
    bitmapImage.CreateOptions = BitmapCreateOptions.None; 
    var bmp = new WriteableBitmap((BitmapSource) bitmapImage); 
    bitmapImage.UriSource = (Uri) null; 
    return bmp; 
1

Silverlight的圖像緩存以提高性能。使用BitmapImage處理資源後,應該調用 image.UriSource = null

+0

我試過這個,但結果相同。 – StezPet 2013-04-25 07:48:33

1

你正在重置/處置的IsolatedStorageFileStream並使用後他們IsolatedStorageFile

1

你有沒有試過迫使垃圾收集器運行,看看是否有任何區別。

GC.Collect(); 

這不應作爲一種解決方案 - 你永遠不應該叫GC.Collect,但它可能會幫助確定問題是否是被回收內存泄漏,或只是一個延遲的內存。