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

C# 从图片数组中检索图像属性,c#,date,properties,time,photo,C#,Date,Properties,Time,Photo,我目前正在使用C#开发一个Windows窗体应用程序。我想知道如何检索创建图像的日期和时间。我在线查看了解决方案,找到了PropertyItem的引用,但我没有 了解如何使用它 下面的代码从文件夹中拍摄照片并将其显示在图片数组中。单击图片时,如何在消息框中显示创建图片的日期和时间 // Function to add PictureBox Controls private void AddControls(int cNumber) { imgArray =

我目前正在使用C#开发一个Windows窗体应用程序。我想知道如何检索创建图像的日期和时间。我在线查看了解决方案,找到了PropertyItem的引用,但我没有 了解如何使用它

下面的代码从文件夹中拍摄照片并将其显示在图片数组中。
单击图片时,如何在消息框中显示创建图片的日期和时间

    // 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;
    }
//用于添加PictureBox控件的函数
专用void AddControls(int cNumber)
{
imgArray=new System.Windows.Forms.PictureBox[cNumber];//分配数字数组
对于(int i=0;i360)//一行中有六个图像
{
Xpos=27;//在左侧保留8个像素
Ypos=Ypos+72;//图像高度+8
}
Imgaray[i]。左=Xpos;
imgaray[i].Top=Ypos;
imgaray[i].宽度=64;
Imgaray[i].高度=64;
imgaray[i].Visible=true;
//用图像的名称和完整路径填充(标记)
imgaray[i].Tag=imgName[i];
imgArray[i].单击+=新建系统.EventHandler(单击图像);
this.LogsTabPage.Controls.Add(imgArray[i]);
Xpos=Xpos+72;//图像宽度+8
Application.DoEvents();
MyProgress.PerformStep();
打破
}
}
MyProgress.Visible=false;
}

一个
图像一旦加载,就与其原始文件没有任何关系。如果用户单击时,您能够检索图片的原始文件名,则可以使用:

var lastEditDate = File.GetLastWriteTime("file.jpg");
var lastEditDate = new FileInfo("file.jpg").LastWriteTime;
或者,如果您已经使用了
FileInfo
实例,则可以使用:

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

由于不能从
图像
派生,因为它的构造函数是内部的,所以可以在某处保留一个
字典
,在加载图像时填充它,然后通过查找检索单击图像的相对路径。

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


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

是否可以直接从实际文件中检索日期和时间?我该怎么做?@David我是不是误会了什么?我的帖子演示了如何做到这一点。对不起,你怎么能在messagebox中输出这个结果呢?我尝试了==>MessageBox.Show(var lastEditDate=newfileinfo(ImageToShow.LastWriteTime)@David我假设
ImageToShow
是一个字符串,在这种情况下,您可以执行:
MessageBox.Show(File.GetLastWriteTime(ImageToShow.ToString()
)。