Get the date and time the photo was taken down to the second

I have previously written about file property acquisition-related issues in the article GetDetailsOf to get file properties but the date/time attribute can be acquired up to the year, month, hour, and minute, but not the second.

For example, when acquiring the date and time of shooting with GetDetailsOf, you can get the date and time by giving “12” as an argument, but only up to the minute. The following is a sample of how to use GetDetailsOf to obtain the date and time of a photo shoot up to the minute.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get file properties
            GetFileProperty();
        }

        //Get file properties
        [STAThread]
        static void GetFileProperty()
        {
            String targetFolder = "D:\\test";
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(targetFolder);
            System.IO.FileInfo[] files = di.GetFiles("*.*");

            var shellAppType = Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = Activator.CreateInstance(shellAppType);

            foreach (System.IO.FileInfo f in files)
            {
                var strFileName = f.FullName;
                Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
                Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));

                //File name
                String filename = objFolder.GetDetailsOf(folderItem, 0);
                System.Diagnostics.Debug.WriteLine("File name: " + filename);
                //Date taken
                String picDate = objFolder.GetDetailsOf(folderItem, 12);
                System.Diagnostics.Debug.WriteLine("Date taken:" + picDate);
            }
        }

    }
}

The result of this output will look like this

File name: test (1).JPG
Date taken: 2019/10/06 10:53
File name: test (2).JPG
Date taken:2019/10/22 14:19
File name: test (3).JPG
Date taken:2019/10/06 10:55

As shown in the output results, the date and time taken is “2019/10/06 10:55” to the minute. The creation and modification date and time can be obtained using System.IO.FileInfo and

System.IO.FileInfo f = new System.IO.FileInfo(filePath);
Console.WriteLine(“CreationTime:” + f.CreationTime.ToString());
Console.WriteLine(“Modified:” + f.LastWriteTime.ToString());

to get up to the second, but the shooting date and time cannot be obtained in FileInfo. Therefore, the date and time of shooting can be obtained by reading from the Image.

The following is a sample of acquiring the shooting date and time up to the second. Drawing” is added to the reference setting, and the following two lines are added at the top of the file.

using System.Drawing;
using System.Drawing.Imaging;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get the shooting date and time down to the second
            GetPicDateTime();
        }


        //Sample
        static void GetPicDateTime()
        {
            String targetFolder = "D:\\test";
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(targetFolder);
            System.IO.FileInfo[] files = di.GetFiles("*.*");

            foreach (System.IO.FileInfo f in files)
            {
                //File name
                System.Diagnostics.Debug.WriteLine("File name: " + f.Name);
                //Date taken
                String picDate = GetPicDateTimeFromImage(f.FullName);
                System.Diagnostics.Debug.WriteLine("Date taken:" + picDate);
            }
        }

        //Get the shooting date and time down to the second
        public static string GetPicDateTimeFromImage(string filePath)
        {
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    using (Image img = Image.FromStream(fs, false, false))
                    {
                        PropertyItem item = img.GetPropertyItem(36867);
                        //Convert to string
                        string picDateStr = System.Text.Encoding.UTF8.GetString(item.Value).Trim(new char[] { '\0' });
                        //Convert to DateTime
                        DateTime dt = DateTime.ParseExact(picDateStr, "yyyy:MM:dd HH:mm:ss", null);
                        //Set to yyyy/MM/dd HH:mm:ss format
                        picDateStr = dt.ToString("yyyy/MM/dd HH:mm:ss");

                        return picDateStr;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return "";
        }

    }
}

GetPicDateTime calls the GetPicDateTimeFromImage method. The output result is as follows. The time up to 1.5 seconds is obtained.

File name: test (1).JPG
Date taken: 2019/10/06 10:53:08
File name: test (2).JPG
Date taken:2019/10/22 14:19:07
File name: test (3).JPG
Date taken: 2019/10/06 10:55:14

 

Of course, if the file does not contain Exif information in the first place, the date and time taken cannot be obtained, so an empty string is returned.

 

コメント

Copied title and URL