如何使用C#从文件中获取EXIF数据

时间:2022-01-25 01:16:48

I would like to write a small program in C# which goes through my jpeg photos and, for example, sorts them into dated folders (using MY dating conventions, dammit...).

我想在C#中编写一个小程序,通过我的jpeg照片,例如,将它们分类为过时的文件夹(使用我的约会惯例,该死...)。

Does anyone know a relatively easy way to get at the EXIF data such as Date And Time or Exposure programatically? Thanks!

有没有人知道以编程方式获取日期和时间或曝光等EXIF数据的相对简单的方法?谢谢!

7 个解决方案

#1


13  

Check out this metadata extractor. It is written in Java but has also been ported to C#. I have used the Java version to write a small utility to rename my jpeg files based on the date and model tags. Very easy to use.

查看此元数据提取器。它是用Java编写的,但也被移植到C#。我使用Java版本编写了一个小实用程序,根据日期和模型标签重命名我的jpeg文件。非常好用。


EDIT metadata-extractor supports .NET too. It's a very fast and simple library for accessing metadata from images and videos.

EDIT元数据提取器也支持.NET。它是一个非常快速和简单的库,用于从图像和视频访问元数据。

It fully supports Exif, as well as IPTC, XMP and many other types of metadata from file types including JPEG, PNG, GIF, PNG, ICO, WebP, PSD, ...

它完全支持Exif,以及IPTC,XMP和来自文件类型的许多其他类型的元数据,包括JPEG,PNG,GIF,PNG,ICO,WebP,PSD,......

var directories = ImageMetadataReader.ReadMetadata(imagePath);

// print out all metadata
foreach (var directory in directories)
foreach (var tag in directory.Tags)
    Console.WriteLine($"{directory.Name} - {tag.Name} = {tag.Description}");

// access the date time
var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
var dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTime);

It's available via NuGet and the code's on GitHub.

它可以通过NuGet和GitHub上的代码获得。

#2


42  

As suggested, you can use some 3rd party library, or do it manually (which is not that much work), but the simplest and the most flexible is to perhaps use the built-in functionality in .NET. For more see:

正如所建议的,您可以使用某些第三方库,或者手动执行(这不是那么多工作),但最简单和最灵活的可能是使用.NET中的内置功能。欲了解更多信息:

I say "it’s the most flexible" because .NET does not try to interpret or coalesce the data in any way. For each EXIF you basically get an array of bytes. This may be good or bad depending on how much control you actually want.

我说“它是最灵活的”,因为.NET不会试图以任何方式解释或合并数据。对于每个EXIF,您基本上都得到一个字节数组。根据您实际需要的控制程度,这可能是好的也可能是坏的。

Also, I should point out that the property list does not in fact directly correspond to the EXIF values. EXIF itself is stored in multiple tables with overlapping ID’s, but .NET puts everything in one list and redefines ID’s of some items. But as long as you don’t care about the precise EXIF ID’s, you should be fine with the .NET mapping.

另外,我应该指出,属性列表实际上并不直接对应于EXIF值。 EXIF本身存储在具有重叠ID的多个表中,但.NET将所有内容放在一个列表中并重新定义某些项的ID。但只要您不关心精确的EXIF ID,您就可以使用.NET映射了。

#3


9  

Here is a link to another similar SO question, which has an answer pointing to this good article on "Reading, writing and photo metadata" in .Net.

这是一个链接到另一个类似的SO问题,答案指向.Net中关于“阅读,写作和照片元数据”的这篇好文章。

#4


6  

You can use TagLib# which is used by applications such as F-Spot. Besides Exif, it will read a good amount of metadata formats for image, audio and video.

您可以使用由F-Spot等应用程序使用的TagLib#。除了Exif之外,它还会读取大量的图像,音频和视频元数据格式。

I also like ExifUtils API but it is buggy and is not actively developed.

我也喜欢ExifUtils API,但它有缺陷并且没有积极开发。

#5


5  

Image class has PropertyItems and PropertyIdList properties. You can use them.

Image类具有PropertyItems和PropertyIdList属性。你可以使用它们。

#6


3  

The command line tool ExifTool by Phil Harvey works with dozens of images formats - including plenty of proprietary RAW formats - and can manipulate a variety of metadata formats including EXIF, GPS, IPTC, XMP, JFIF.

Phil Harvey的命令行工具ExifTool使用了许多图像格式 - 包括大量专有的RAW格式 - 并且可以操作各种元数据格式,包括EXIF,GPS,IPTC,XMP,JFIF。

Very easy to use, lightweight, impressive application.

非常好用,轻巧,令人印象深刻的应用程序。

#7


3  

Getting EXIF data from a JPEG image involves:

从JPEG图像获取EXIF数据涉及:

  1. Seeking to the JPEG markers which mentions the beginning of the EXIF data,. e.g. normally oxFFE1 is the marker inserted while encoding EXIF data, which is a APPlication segment, where EXIF data goes.
  2. 寻求提到EXIF数据开头的JPEG标记。例如通常,oxFFE1是编码EXIF数据时插入的标记,EXIF数据是EXIF数据所在的APPlication段。

  3. Parse all the data from say 0xFFE1 to 0xFFE2 . This data would be stream of bytes, in the JPEG encoded file.
  4. 将所有数据从0xFFE1解析为0xFFE2。该数据将是JPEG编码文件中的字节流。

  5. ASCII equivalent of these bytes would contain various information related to Image Date, Camera Model Name, Exposure etc...
  6. 这些字节的ASCII等价物将包含与图像日期,相机型号名称,曝光等相关的各种信息......

#1


13  

Check out this metadata extractor. It is written in Java but has also been ported to C#. I have used the Java version to write a small utility to rename my jpeg files based on the date and model tags. Very easy to use.

查看此元数据提取器。它是用Java编写的,但也被移植到C#。我使用Java版本编写了一个小实用程序,根据日期和模型标签重命名我的jpeg文件。非常好用。


EDIT metadata-extractor supports .NET too. It's a very fast and simple library for accessing metadata from images and videos.

EDIT元数据提取器也支持.NET。它是一个非常快速和简单的库,用于从图像和视频访问元数据。

It fully supports Exif, as well as IPTC, XMP and many other types of metadata from file types including JPEG, PNG, GIF, PNG, ICO, WebP, PSD, ...

它完全支持Exif,以及IPTC,XMP和来自文件类型的许多其他类型的元数据,包括JPEG,PNG,GIF,PNG,ICO,WebP,PSD,......

var directories = ImageMetadataReader.ReadMetadata(imagePath);

// print out all metadata
foreach (var directory in directories)
foreach (var tag in directory.Tags)
    Console.WriteLine($"{directory.Name} - {tag.Name} = {tag.Description}");

// access the date time
var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
var dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTime);

It's available via NuGet and the code's on GitHub.

它可以通过NuGet和GitHub上的代码获得。

#2


42  

As suggested, you can use some 3rd party library, or do it manually (which is not that much work), but the simplest and the most flexible is to perhaps use the built-in functionality in .NET. For more see:

正如所建议的,您可以使用某些第三方库,或者手动执行(这不是那么多工作),但最简单和最灵活的可能是使用.NET中的内置功能。欲了解更多信息:

I say "it’s the most flexible" because .NET does not try to interpret or coalesce the data in any way. For each EXIF you basically get an array of bytes. This may be good or bad depending on how much control you actually want.

我说“它是最灵活的”,因为.NET不会试图以任何方式解释或合并数据。对于每个EXIF,您基本上都得到一个字节数组。根据您实际需要的控制程度,这可能是好的也可能是坏的。

Also, I should point out that the property list does not in fact directly correspond to the EXIF values. EXIF itself is stored in multiple tables with overlapping ID’s, but .NET puts everything in one list and redefines ID’s of some items. But as long as you don’t care about the precise EXIF ID’s, you should be fine with the .NET mapping.

另外,我应该指出,属性列表实际上并不直接对应于EXIF值。 EXIF本身存储在具有重叠ID的多个表中,但.NET将所有内容放在一个列表中并重新定义某些项的ID。但只要您不关心精确的EXIF ID,您就可以使用.NET映射了。

#3


9  

Here is a link to another similar SO question, which has an answer pointing to this good article on "Reading, writing and photo metadata" in .Net.

这是一个链接到另一个类似的SO问题,答案指向.Net中关于“阅读,写作和照片元数据”的这篇好文章。

#4


6  

You can use TagLib# which is used by applications such as F-Spot. Besides Exif, it will read a good amount of metadata formats for image, audio and video.

您可以使用由F-Spot等应用程序使用的TagLib#。除了Exif之外,它还会读取大量的图像,音频和视频元数据格式。

I also like ExifUtils API but it is buggy and is not actively developed.

我也喜欢ExifUtils API,但它有缺陷并且没有积极开发。

#5


5  

Image class has PropertyItems and PropertyIdList properties. You can use them.

Image类具有PropertyItems和PropertyIdList属性。你可以使用它们。

#6


3  

The command line tool ExifTool by Phil Harvey works with dozens of images formats - including plenty of proprietary RAW formats - and can manipulate a variety of metadata formats including EXIF, GPS, IPTC, XMP, JFIF.

Phil Harvey的命令行工具ExifTool使用了许多图像格式 - 包括大量专有的RAW格式 - 并且可以操作各种元数据格式,包括EXIF,GPS,IPTC,XMP,JFIF。

Very easy to use, lightweight, impressive application.

非常好用,轻巧,令人印象深刻的应用程序。

#7


3  

Getting EXIF data from a JPEG image involves:

从JPEG图像获取EXIF数据涉及:

  1. Seeking to the JPEG markers which mentions the beginning of the EXIF data,. e.g. normally oxFFE1 is the marker inserted while encoding EXIF data, which is a APPlication segment, where EXIF data goes.
  2. 寻求提到EXIF数据开头的JPEG标记。例如通常,oxFFE1是编码EXIF数据时插入的标记,EXIF数据是EXIF数据所在的APPlication段。

  3. Parse all the data from say 0xFFE1 to 0xFFE2 . This data would be stream of bytes, in the JPEG encoded file.
  4. 将所有数据从0xFFE1解析为0xFFE2。该数据将是JPEG编码文件中的字节流。

  5. ASCII equivalent of these bytes would contain various information related to Image Date, Camera Model Name, Exposure etc...
  6. 这些字节的ASCII等价物将包含与图像日期,相机型号名称,曝光等相关的各种信息......