元数据有时会返回完全不正确的日期创建

时间:2013-08-11 02:32:25

标签: c# image metadata

我得到了用C#创建图片的日期。这是我正在使用的方法。

foreach (PropertyItem propItem in image.PropertyItems)
{
    if (propItem.Id == 0x0132)
    {
        date = (new System.Text.ASCIIEncoding().GetString(propItem.Value));
        MessageBox.Show("The picture " + file + " was taken at " + date);
        image.Dispose();
        date = date.Substring(0, 4);
        monthstring = getPath + "\\" + date;
        if (!Directory.Exists(monthstring))
        {
            Directory.CreateDirectory(monthstring);
        }
        File.Move(file, monthstring + "\\" + filetype);
        progressBar1.PerformStep();
    }
}

现在,这种方法有时会起作用。有些时候,它会返回完全错误的日期!有什么我做错了吗?有时它说它是在今天拍摄的,当它在2005年拍摄时。有时它也给了我错误的月份。大部分时间都是搞砸了。

1 个答案:

答案 0 :(得分:1)

试试这个:

// Get the Date Created property 
System.Drawing.Imaging.PropertyItem propertyItem = image.GetPropertyItem( 0x132 ); 
if( propItem != null ) 
{ 
  // Extract the property value as a String. 
  System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
  string text = encoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1 ); 

  // Parse the date and time. 
  System.Globalization.CultureInfo provider = CultureInfo.InvariantCulture; 
  DateTime dateCreated = DateTime.ParseExact( text, "yyyy:MM:d H:m:s", provider ); 
}

原始答案:

“Date Taken” not showing up in Image PropertyItems

How can I find out when a picture was actually taken in C#