2014-02-25 18 views
3

我有圖像的流,並且我將其轉換成圖像由下面的代碼如何在Image中使用它之後處理流?

Image imagefromstream = Image.FromStream(stream); 

當我在繪製的圖形這個圖像時,繪製圖像。

但是,如果我在繪製圖形後處理流,圖像不會繪製。

誰能幫我處理圖像流

+0

顯示你如何繪製圖像和流處置更多的代碼請。 –

+1

如果這實際上是關於WPF的,那麼根本不應該使用'System.Drawing.Image',因爲它是WinForms。相反,使用'System.Windows.Media.Imaging.BitmapImage',如Sankarann給出的答案所示。 – Clemens

回答

7

根據MSDN here

You must keep the stream open for the lifetime of the Image.

你必須當你關閉你的應用程序處置流(或當你不再需要圖片)

2

在這種情況下,你有直接參考內存流,如果你把它放置,圖像也將被處置。

所以你從流源,並讓它在BitmapImage,設置BitmapImage作爲源圖像..

var imageSource = new BitmapImage(); 
imageSource.BeginInit(); 
imageSource.StreamSource = memoryStream; 
imageSource.CacheOption= CacheOption.OnLoad; 
imageSource.EndInit(); 

// Assign the Source property of your image 
image.Source = imageSource; 
+2

這也適用於其他流,不僅僅是MemoryStream。您可以在'EndInit'之後安全地關閉流。 – Clemens

+0

+1爲一個很好的解決方案 – Baldrick

相關問題