2012-06-16 25 views
1

我有一個問題,在我的應用程序米使用或顯示圖像的GridView其保存在我的應用程序的本地文件夾,我想刪除通過選擇它們這些圖片,但問題是,當打開或使用任何東西(文件或圖像)時,這些圖像正在使用,並在窗口中顯示,因此它不應從驅動器中刪除。我想從硬盤上刪除圖像。 m在GridView中通過BitmapImage中的URI路徑使用或訪問這些圖像。這樣刪除存儲文件(圖片)C#地鐵應用

私人的ImageSource _image = NULL; this._image =新的BitmapImage(新URI(新URI( 「MS-應用程序數據:///本地/ RecentImages /」),this._imagePath));

而m試圖刪除 請告訴我如何停止或配置GridView和StorageFile之間的鏈接 或如何從存儲設備中刪除映像?

+0

當您打開圖像時,在讀取圖像數據後,關閉圖像文件流。 – Code0987

+0

如何關閉圖像流先生? – Sajid

+0

這些圖像在我的GridView作爲最近的活動,這些都是我的主要 – Sajid

回答

0

我假設在同一時間沒有其他應用程序加載相同的圖像。 這是一種在將圖像文件載入內存後關閉圖像文件流的方法。

/// <summary> 
    /// Gets the bitmap. 
    /// </summary> 
    /// <param name="path">The path.</param> 
    /// <returns></returns> 
    public static BitmapSource GetBitmap(string path) 
    { 
     if (!File.Exists(path)) return null; 

     MemoryStream ms = new MemoryStream(); 
     BitmapImage bi = new BitmapImage(); 
     byte[] bytArray = File.ReadAllBytes(path); 
     ms.Write(bytArray, 0, bytArray.Length); ms.Position = 0; 
     bi.BeginInit(); 
     bi.StreamSource = ms; 
     bi.EndInit(); 
     return bi; 
    } 
+0

非常感謝您的先生 – Sajid