2015-08-14 33 views
0

我正在從圖像中獲取圖像創建日期。我注意到,有些圖像會導致我Property cannot be found.錯誤,而相同的代碼適用於其他圖像。獲取圖像創建日期時無法找到屬性

對於前, 我測試了我的代碼約40圖像,我沒有發現任何問題,但現在我在不同的圖像集上測試相同的代碼,所有這些圖像都引發此錯誤。

代碼:

private DateTime GetDateTakenFromImage(string path) 
{ 
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) 
    using (Image myImage = Image.FromStream(fs, false, false)) 
    { 
     PropertyItem propItem = myImage.GetPropertyItem(36867);//Throws error here 
     string dateTaken = new Regex(":").Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2); 
     return DateTime.Parse(dateTaken); 
    } 
} 

我已經檢查到的圖像性質的工作和非工作圖像,但我看不出有什麼差別。

我在這裏錯過了什麼?

+0

如果這是jpeg EXIF信息,我會尋找EXIF閱讀代碼。 – weston

回答

1

該屬性並不總是存在。首先檢查以下幾行:

if (myImage.PropertyIdList.Any(p => p == 36867)) 
+0

在這種情況下,我如何獲得圖像創建日期?有了這個條件,它會從循環中走出來。如果有什麼方法可以請給我一個示例代碼? – CSharper

+0

如果36867爲空,則您無法獲取原始圖像創建日期。 – Antrim