从图片数组中检索图像属性

时间:2013-01-02 05:33:42

标签: c# date properties time photo

我目前正在使用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 个答案:

答案 0 :(得分:2)

Image一旦加载,与其原始文件没有任何关系。如果,当用户点击时,您可以检索图片的原始文件名,您可以使用:

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

或者,如果您已经使用FileInfo个实例,则可以使用:

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

由于您无法从Image派生,因为它的构造函数是内部的,您可以在某处保留Dictionary<Image,string>,在加载图像时填充它,然后检索所点击图像的相对路径通过查找。

答案 1 :(得分:0)

至少jpeg文件附加了元数据。请不要:它可能是附件,但你不能确保它在那里。

您已经在阅读图像(Image.FromFile),结果对象支持方法GetPropertyItem。使用该属性,您可以读出元数据。有一个可用的properties列表(0x0132应该是日期和时间)和sample