Read Photo Taken Date
-
-
How can I read the photo taken date(not the created date or modified date). I found on Google that can be do by propertyItems. There’s no date taken property in Property items. Is there any possible way to read the photo taken date??? :confused:
A S E L A
Some file formats (including JPEG) allow for a lot of metadata, and most camera's seem to provide at least some of these pieces of information. Here is some code that may be useful to you; I will not provide support on it though.
string s=readImageProperty(image, 0x132); // ASCII log("datetime="+s); DateTime exposureDate=ParseExposureDate(s, equipModel); log("Exposure DateTime = "+DateToString(exposureDate, false, ""));
// reads a specific property from an image, and returns it as a string
// (whatever format it originally had);
// returns null when property is not available.
// typically only works if image got loaded from a JPEG file.
private static string readImageProperty(Image image, int ID) {
try {
PropertyItem pi=image.GetPropertyItem(ID); // DateTime
if(pi!=null) {
if(pi.Type==2) { // ASCII
return ASCIIencoding.GetString(pi.Value, 0, pi.Len-1);
}
if(pi.Type==5) { // rational
byte[] bb=pi.Value;
uint uNominator=BitConverter.ToUInt32(bb, 0);
uint uDenominator=BitConverter.ToUInt32(bb, 4);
if(uDenominator==1) return uNominator.ToString();
return uNominator.ToString()+"/"+uDenominator.ToString();
}
}
} catch { // catch and ignore all kinds of exceptions, such as "no such property"
}
return null;
}// Converts a JPEG datetime property to a standard DateTime;
// may try several known formats, and returns DateTime.MinValue on failure.
// REMARK: if necessary, make the format candidates depend on
// (part of) the equipment model
private static DateTime ParseExposureDate(string exposure, string equipModel) {
DateTime dt=DateTime.MinValue;
bool OK=false;
// repeat the next IF block, with different arguments, for each
// possible DateTime format (or each camera)
if(!OK) {
// NIKON D70s gives: "2007:04:07 09:02:00"
// Canon PowerShot A620 gives: "2006:09:25 18:43:33"
// Olympus C460 gives: "2006:03:12 15:49:54"
// Olympus FE200 gives: "2007:03:17 01:18:30"
OK=DateTime.TryParseExact(exposure, "yyyy:MM:dd HH:mm:ss",
null, DateTimeStyles.None, out dt);
}
log("ParseExposureDate: \""+exposure+"\" >>> "+dt);
return dt;
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your que
-
Some file formats (including JPEG) allow for a lot of metadata, and most camera's seem to provide at least some of these pieces of information. Here is some code that may be useful to you; I will not provide support on it though.
string s=readImageProperty(image, 0x132); // ASCII log("datetime="+s); DateTime exposureDate=ParseExposureDate(s, equipModel); log("Exposure DateTime = "+DateToString(exposureDate, false, ""));
// reads a specific property from an image, and returns it as a string
// (whatever format it originally had);
// returns null when property is not available.
// typically only works if image got loaded from a JPEG file.
private static string readImageProperty(Image image, int ID) {
try {
PropertyItem pi=image.GetPropertyItem(ID); // DateTime
if(pi!=null) {
if(pi.Type==2) { // ASCII
return ASCIIencoding.GetString(pi.Value, 0, pi.Len-1);
}
if(pi.Type==5) { // rational
byte[] bb=pi.Value;
uint uNominator=BitConverter.ToUInt32(bb, 0);
uint uDenominator=BitConverter.ToUInt32(bb, 4);
if(uDenominator==1) return uNominator.ToString();
return uNominator.ToString()+"/"+uDenominator.ToString();
}
}
} catch { // catch and ignore all kinds of exceptions, such as "no such property"
}
return null;
}// Converts a JPEG datetime property to a standard DateTime;
// may try several known formats, and returns DateTime.MinValue on failure.
// REMARK: if necessary, make the format candidates depend on
// (part of) the equipment model
private static DateTime ParseExposureDate(string exposure, string equipModel) {
DateTime dt=DateTime.MinValue;
bool OK=false;
// repeat the next IF block, with different arguments, for each
// possible DateTime format (or each camera)
if(!OK) {
// NIKON D70s gives: "2007:04:07 09:02:00"
// Canon PowerShot A620 gives: "2006:09:25 18:43:33"
// Olympus C460 gives: "2006:03:12 15:49:54"
// Olympus FE200 gives: "2007:03:17 01:18:30"
OK=DateTime.TryParseExact(exposure, "yyyy:MM:dd HH:mm:ss",
null, DateTimeStyles.None, out dt);
}
log("ParseExposureDate: \""+exposure+"\" >>> "+dt);
return dt;
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your que
-
browsing the documentation for 10 seconds would reveal the namespace. I am not in the spoon feeding business, I try to provide added value. :~
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets