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

发布于 2024-07-05 05:28:29 字数 134 浏览 10 评论 0 原文

我想用 C# 编写一个小程序,它可以浏览我的 jpeg 照片,例如,将它们分类到带日期的文件夹中(使用我的约会约定,该死......)。

有谁知道以编程方式获取 EXIF 数据(例如日期和时间或曝光)的相对简单的方法吗? 谢谢!

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...).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

幸福还没到 2024-07-12 05:28:29

正如建议的那样,您可以使用一些第三方库,或者手动完成(这不是很多工作),但最简单和最灵活的可能是使用 .NET 中的内置功能。 有关详细信息,请参阅:

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

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


编辑:可以在不加载以下答案后的完整图像的情况下完成此操作:https://stackoverflow.com /a/552642/2097240

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:

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.

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.


Edit: It's possible to do it without loading the full image following this answer: https://stackoverflow.com/a/552642/2097240

你不是我要的菜∠ 2024-07-12 05:28:29

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

  1. 查找提及 EXIF 数据开头的 JPEG 标记。 例如,通常 oxFFE1 是在编码 EXIF 数据时插入的标记,它是 EXIF 数据所在的 APPlication 段。
  2. 解析从 0xFFE1 到 0xFFE2 的所有数据。 该数据将是 JPEG 编码文件中的字节流。
  3. 这些字节的 ASCII 等效项将包含与图像日期、相机型号名称、曝光等相关的各种信息...

Getting EXIF data from a JPEG image involves:

  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. Parse all the data from say 0xFFE1 to 0xFFE2 . This data would be stream of bytes, in the JPEG encoded file.
  3. ASCII equivalent of these bytes would contain various information related to Image Date, Camera Model Name, Exposure etc...
绻影浮沉 2024-07-12 05:28:29

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

非常易于使用,轻量级,令人印象深刻的应用程序。

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.

Very easy to use, lightweight, impressive application.

逆夏时光 2024-07-12 05:28:29

最近,我使用了这个 .NET 元数据 API。 我还写了 关于它的博客文章,其中展示了使用 C# 读取、更新和删除图像中的 EXIF 数据。

using (Metadata metadata = new Metadata("image.jpg"))
{
    IExif root = metadata.GetRootPackage() as IExif;
    if (root != null && root.ExifPackage != null)
    {
        Console.WriteLine(root.ExifPackage.DateTime);
     }
}

Recently, I used this .NET Metadata API. I have also written a blog post about it, that shows reading, updating, and removing the EXIF data from images using C#.

using (Metadata metadata = new Metadata("image.jpg"))
{
    IExif root = metadata.GetRootPackage() as IExif;
    if (root != null && root.ExifPackage != null)
    {
        Console.WriteLine(root.ExifPackage.DateTime);
     }
}
谈下烟灰 2024-07-12 05:28:29

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

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

今天小雨转甜 2024-07-12 05:28:29

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.

数理化全能战士 2024-07-12 05:28:29

最快的方法是使用 windows api 编解码器 不打开文件而是使用缓存的exif信息

var prop = ShellFile.FromFilePath(f).Properties;
var Dimensions = prop.GetProperty("Dimensions").ValueAsObject.ToString(); 
//1280 x 800

fastest way is to use windows api codec that doesn't open file and instead uses cached exif information

var prop = ShellFile.FromFilePath(f).Properties;
var Dimensions = prop.GetProperty("Dimensions").ValueAsObject.ToString(); 
//1280 x 800

长伴 2024-07-12 05:28:29

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


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

它完全支持 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);

可通过 NuGet代码在 GitHub 上

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.


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

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, ...

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.

阪姬 2024-07-12 05:28:29

您可以使用 TagLib#,它由 F-点。 除了 Exif 之外,它还能读取大量图像、音频和视频的元数据格式。

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文