如何使用 C# 从文件中获取 EXIF 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the EXIF data from a file using C#
提问by Joel in G?
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 数据(例如日期和时间或曝光)的相对简单的方法?谢谢!
采纳答案by Dave Griffiths
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 文件。非常容易使用。
EDITmetadata-extractorsupports .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 NuGetand the code's on GitHub.
它可以通过NuGet 获得,代码在 GitHub 上。
回答by goldenmean
Getting EXIF data from a JPEG image involves:
从 JPEG 图像中获取 EXIF 数据包括:
- 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.
- Parse all the data from say 0xFFE1 to 0xFFE2 . This data would be stream of bytes, in the JPEG encoded file.
- ASCII equivalent of these bytes would contain various information related to Image Date, Camera Model Name, Exposure etc...
- 寻找提到 EXIF 数据开头的 JPEG 标记。例如,通常 oxFFE1 是在编码 EXIF 数据时插入的标记,这是一个应用程序段,EXIF 数据在其中。
- 解析从 0xFFE1 到 0xFFE2 的所有数据。此数据将是 JPEG 编码文件中的字节流。
- 这些字节的 ASCII 等价物将包含与图像日期、相机型号名称、曝光等相关的各种信息......
回答by idursun
Image class has PropertyItems and PropertyIdList properties. You can use them.
Image 类具有 PropertyItems 和 PropertyIdList 属性。你可以使用它们。
回答by Joel in G?
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 中“阅读、写作和照片元数据”的好文章。
回答by Jan Zich
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:
正如建议的那样,您可以使用一些 3rd 方库,或者手动完成(这不是很多工作),但最简单和最灵活的方法是使用 .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 映射。
Edit:It's possible to do it without loading the full image following this answer: https://stackoverflow.com/a/552642/2097240
编辑:可以在不加载完整图像的情况下按照以下答案进行操作:https: //stackoverflow.com/a/552642/2097240
回答by smola
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 ExifUtilsAPI but it is buggy and is not actively developed.
我也喜欢ExifUtilsAPI,但它有问题并且没有积极开发。
回答by Kirk Broadhurst
The command line tool ExifTool by Phil Harveyworks 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.
非常易于使用,重量轻,令人印象深刻的应用程序。