private void button1_Click(object sender, EventArgs e)
{
pathBrowser.ShowDialog();
DirectoryInfo di = new DirectoryInfo(pathBrowser.SelectedPath);
FileInfo[] rgFiles = di.GetFiles("*.jpg");
foreach (FileInfo fi in rgFiles)
{
//Combine full path\file
string fname = di.ToString() + "\\" + fi.Name;
//Read in image
Image i = Image.FromFile(fname);
//Get date/time photo was taken
DateTime dt = GetDateTaken(i);
//string dt1 = dt.ToLongTimeString();//full time string
string dt2 = dt1.Substring(0, dt1.Length - 3);//time without AM/PM
int ind1 = dt2.IndexOf(":",0);//find first colon
int ind2 = dt2.IndexOf(":", ind1+1);//find second colon
//Convert strings to integers
string hh = dt2.Substring(0, ind1);
string mm = dt2.Substring(ind1+1, ind2-ind1-1);
string ss = dt2.Substring(ind2 + 1, 2);
Int32 hours = Convert.ToInt32(hh);
Int32 minutes = Convert.ToInt32(mm);
Int32 seconds = Convert.ToInt32(ss);
//Account for AM/PM
int ind=0;
if (AMPM != "AM")
ind=ind+12;
//Calculate total seconds since midnight
Int32 TotalTime = 3600 * (hours+ind) + 60 * minutes + seconds;
}
}
public DateTime GetDateTaken(Image targetImg)
{
//Property Item 36867 corresponds to the Date Taken
PropertyItem propItem = targetImg.GetPropertyItem(36867);
DateTime dtaken;
//Convert date taken metadata to a DateTime object
string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
string firsthalf = sdate.Substring(0, 10);
firsthalf = firsthalf.Replace(":", "-");
sdate = firsthalf + secondhalf;
dtaken = DateTime.Parse(sdate);
return dtaken;
}