2011-03-22 74 views
0

我的應用程序包含一個Image控件,它具有綁定到磁盤映像文件的功能。我有一些條件,圖像文件需要更新。但更新無法完成,因爲圖像文件已打開且無法覆蓋。我該怎麼辦?如何更新圖像文件綁定到圖像控件?

回答

0

你可以嘗試刪除綁定,因此圖像不會被你的程序 可以使用除了覆蓋圖像文件 比重新添加結合

我不知道這一點,但它是值得一試

+0

它不起作用。我試過'BindingOperations.ClearBinding(); img.Source = null; GC.Collect()'但圖像仍然是寫保護的。 – Hugo 2011-03-22 03:42:13

0

現在我的解決方案是: 使用轉換器將圖像路徑轉換爲BitmapImage。 ,使用FileStream加載圖像並將數據複製到MemoryStream中,最後關閉FileStream。

 BitmapImage bmp = new BitmapImage(); 
     bmp.CacheOption = BitmapCacheOption.OnLoad; 
     bmp.BeginInit(); 
     var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); 
     var memStream = new MemoryStream(); 
     memStream.SetLength(fileStream.Length); 
     fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length); 
     memStream.Flush(); 
     fileStream.Close(); 
     bmp.StreamSource = memStream; 
     bmp.EndInit(); 
     return bmp; 
相關問題