2013-01-02 51 views
1

我目前正在使用C#處理Windows窗體應用程序。我想知道如何檢索圖像的創建日期和時間。我在網上查瞭解,發現對PropertyItem的引用,但我不知道如何使用它。從圖片數組中檢索圖像屬性

我的代碼如下從一個文件夾中獲取照片並將它們顯示在一個圖片數組中。
如何在單擊時在MessageBox中創建圖片的日期和時間?

// Function to add PictureBox Controls 
    private void AddControls(int cNumber) 
    { 
     imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array 
     for (int i = 0; i < cNumber; i++) 
     { 
      imgArray[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable 
     } 
     // When call this function you determine number of controls 
    } 

    private void ClickImage(Object sender, System.EventArgs e) 
    { 
     // On Click: load (ImageToShow) with (Tag) of the image 
     ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString(); 
     // then view this image on the form (frmView) 

     PrivacyDefenderTabControl.SelectedIndex = 5; 
     LogsPhotosPictureBox.Image = Image.FromFile(ImageToShow); 
     LogsPhotosPictureBox.Left = (this.Width - LogsPhotosPictureBox.Width)/15; 
    } 

    private void ImagesInFolder() 
    { 
     FileInfo FInfo; 
     // Fill the array (imgName) with all images in any folder 
     imgName = Directory.GetFiles(Application.StartupPath + @"\Faces"); 
     // How many Picture files in this folder 
     NumOfFiles = imgName.Length; 
     imgExtension = new string[NumOfFiles]; 
     for (int i = 0; i < NumOfFiles; i++) 
     { 
      FInfo = new FileInfo(imgName[i]); 
      imgExtension[i] = FInfo.Extension; // We need to know the Extension 
     } 
    } 

    private void ShowFolderImages() 
    { 
     int Xpos = 27; 
     int Ypos = 8; 
     Image img; 
     Image.GetThumbnailImageAbort myCallback = 
      new Image.GetThumbnailImageAbort(ThumbnailCallback); 
     MyProgress.Visible = true; 
     MyProgress.Minimum = 0; 
     MyProgress.Maximum = NumOfFiles; 
     MyProgress.Value = 0; 
     MyProgress.Step = 1; 
     string[] Ext = new string[] { ".GIF", ".JPG", ".BMP", ".PNG" }; 
     AddControls(NumOfFiles); 
     for (int i = 0; i < NumOfFiles; i++) 
     { 
      switch (imgExtension[i].ToUpper()) 
      { 
       case ".JPG": 
       case ".BMP": 
       case ".GIF": 
       case ".PNG": 
        img = Image.FromFile(imgName[i]); // or img = new Bitmap(imgName[i]); 
        imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); 
        img = null; 
        if (Xpos > 360) // six images in a line 
        { 
         Xpos = 27; // leave eight pixels at Left 
         Ypos = Ypos + 72; // height of image + 8 
        } 
        imgArray[i].Left = Xpos; 
        imgArray[i].Top = Ypos; 
        imgArray[i].Width = 64; 
        imgArray[i].Height = 64; 
        imgArray[i].Visible = true; 
        // Fill the (Tag) with name and full path of image 
        imgArray[i].Tag = imgName[i]; 
        imgArray[i].Click += new System.EventHandler(ClickImage); 
        this.LogsTabPage.Controls.Add(imgArray[i]); 
        Xpos = Xpos + 72; // width of image + 8 
        Application.DoEvents(); 
        MyProgress.PerformStep(); 
        break; 
      } 
     } 
     MyProgress.Visible = false; 
    } 

回答

2

一旦加載,Image與其原始文件沒有任何關係。如果,當用戶點擊,你就能夠檢索圖片的原始文件名,你可以使用:

var lastEditDate = File.GetLastWriteTime("file.jpg"); 

另外,如果您已經使用FileInfo情況下,你可以使用:

var lastEditDate = new FileInfo("file.jpg").LastWriteTime; 

由於您不能從Image派生,因爲它的構造函數是內部的,您可以在某處保留Dictionary<Image,string>,在加載圖像時填充它,然後通過查找檢索點擊圖像的路徑相對。

+0

是否可以直接從實際文件中檢索日期和時間?我該怎麼做? – David

+0

@大衛我誤解了什麼?我的文章說明了如何去做。 – Mir

+0

對不起,但你怎麼能輸出這個結果在一個消息框?我試過==> MessageBox.Show(var lastEditDate = new FileInfo(ImageToShow).LastWriteTime); – David

0

至少jpeg文件附有元數據。請不要:它可能被附上,但你不能確保它在那裏。

您已經在閱讀圖像(Image.FromFile),並且生成的對象支持方法GetPropertyItem。有了這個屬性,你可以讀出元數據。有一個可用的列表properties(0x0132應該是日期和時間)和sample