2012-03-23 85 views
0
this.Dispatcher.Invoke(
      new Action(() => 
      { 
       if (currentImage != null) 
       { 
        currentImage.Dispose(); 
        currentImage = null; 
       } 
       this.currentImage = render.render(value); 
       this.mainImage.Source = 
        System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
         currentImage.GetHbitmap(), 
         IntPtr.Zero, 
         Int32Rect.Empty, 
         System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
      })); 

注意this.mainImage是WPF中的圖像控制器。 我發現有一些內存泄漏當我執行了句「this.mainImage.Source = XXXX」 我發現沒有辦法釋放被this.mainImage.Source舉行的內存奇怪的C#內存泄漏使用GDI

+1

可能值得把這個改爲一個問題。 – KingCronus 2012-03-23 16:42:15

回答

0

不幸的是沒有直線前進發布Image.Source分配的資源的方式,不是我所知道的。但有不同的方法可以避免它,儘可能多

可能的解決方案之一,當您知道您的不會操縱該元素時,使其成爲Freezable。在這種情況下,WPF框架避免跟蹤該對象上的事件,因爲您聲明它是「只讀」的。所以你明顯地改變了你的應用程序的性能。

舊的,但還是不錯的文章:

Finding Memory Leaks in WPF-based applications