I am running a program to retrieve the "date taken" property of an image. The code I have found is mostly from StackOverflow. I am having difficulty with images that do not have a date. I have found that most images do not have a date in fact.
Anyway, the only way I can get the program to run without errors if the date does not exist is by the use of a try/ catch. I feel this is a very bad usage of the try/ catch facility and to make it even worse this is part of a project that will be graded. I do not think the marker will be impressed with it like that!
This is the code:
try
{
Image img = Image.FromFile(imageFilePath); PropertyItem propItem = img.GetPropertyItem(36867); DateTime dTaken; 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); string dateConvert = String.Format("{0:dd MMM yy}", dTaken); dateTakenTextBox.Text = Convert.ToString(dateConvert);
}
catch (ArgumentException)
{
//date does not exist so just carry on
}
All I need to do is just verify that the property actually exists. The exception is thrown on the second line of the main code section: PropertyItem propItem = img.GetPropertyItem(36867); I cannot work out how to detect that before the error actually occurs.