2013-05-21 23 views
1

,我與它的標題和評論在它旁邊顯示的圖像的圖。 當我加載了現有圖像我利用這個代碼:創建的BitmapImage拋出缺少關鍵例外

 this.ArtifactIdentity = image.ArtifactIdentity; 
     this.Comment = image.Comment; 
     this.Name = image.Name; 
     this.MediaIdentity = image.MediaIdentity; 
     this.ImageArray = image.Image; 
     Image = new BitmapImage(); 
     Image.BeginInit(); 
     Image.CacheOption = BitmapCacheOption.None; 
     Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache; 
     Image.StreamSource = new MemoryStream(this.ImageArray); 
     Image.EndInit(); 

當我的execute()它拋出異常缺少參數鍵EndInit。堆棧跟蹤顯示這一點:

at System.Collections.Hashtable.ContainsKey(Object key) 
    at System.Collections.Hashtable.Contains(Object key) 
    at System.Windows.Media.Imaging.ImagingCache.RemoveFromCache(Uri uri, Hashtable table) 
    at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 
    at System.Windows.Media.Imaging.BitmapImage.EndInit() 

因此,誰能告訴我,當我使用的代碼,我看到很多人成功使用,但我得到這個例外,爲什麼我收到此異常???我很茫然!

+0

什麼是確切的異常信息? – SLaks

回答

1

我發現,寫由布拉德利固安捷here,不得不引起加載BitmapImage的例外列表的文章。 SLak所說的是正確的,但在Brad的文章中,他表示下一個異常(無成像組件適合完成...)在Windows 7機器上經常出現。例外情況表明元數據以某種方式損壞。

我解決了一些測試,以確認,但基本上,如果我把字節流,將其保存到臨時文件,然後使用該臨時文件來填充的BitmapImage我可以成功加載它沒有例外。

它是不是最需要的解決方案,但它確實有一兩件事,我需要:它顯示的圖像沒有例外!

+0

我upvoting這一點,因爲有時候一個雜牌組裝電腦是所有你需要讓過去的問題和完成工作。我不喜歡它:) –

9

這是一個錯誤的WPF引起的,在BitmapImage.FinalizeCreation()

if ((CreateOptions & BitmapCreateOptions.IgnoreImageCache) != 0) 
{ 
    ImagingCache.RemoveFromImageCache(uri); 
} 

如果指定IgnoreImageCache,但不從URI加載,它打破。

剛剛擺脫該標誌的;它僅適用於從URL加載時。

+0

當我從選項中刪除時,我得到一個新的例外:找不到適合完成此操作的成像組件。我有bmp,jpg或png作爲可能的來源。這是爲什麼我得到這個例外? –

+0

@SASS_Shooter:這可能是因爲它無法確定加載的格式。什麼是堆棧跟蹤?你正在加載什麼樣的格式? – SLaks

+0

@SASS_Shooter:嘗試'bitmap.CacheOption = BitmapCacheOption.OnLoad';看到http://stackoverflow.com/a/5346766/34397 – SLaks