一、使用 MetadataExtractor 读取 EXIF 信息
1、NuGet 中安装
在 NuGet 中搜索并安装 MetadataExtractor;
2、包信息
我安装后会有两个包:MetadataExtractor 2.0.0 和 XmpCore 5.1.3
3、代码实现
我是创建的 WPF 项目:
private void BTOpen_Click(object sender, RoutedEventArgs e)
{OpenFileDialog openFileDialog1 = new OpenFileDialog();openFileDialog1.InitialDirectory = "c:\\";openFileDialog1.Filter = "JPEG|*.jpg;*.jpeg;*.jpe;*.jfif";openFileDialog1.FilterIndex = 2;openFileDialog1.RestoreDirectory = true;if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){try{string filename = openFileDialog1.FileName;if (File.Exists(filename)){TBFile.Text = filename;IMImg.Source = new BitmapImage(new Uri(filename, UriKind.Absolute));StringBuilder sb = new StringBuilder();var directories = ImageMetadataReader.ReadMetadata(filename);// print out all metadataforeach (var directory in directories)foreach (var tag in directory.Tags)sb.AppendLine($"{directory.Name} - {tag.Name} = {tag.Description}");TBInfo.Text = sb.ToString();}}catch (Exception ex){System.Windows.Forms.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);}}
}
4、效果图
二、简单的读取工具类
1、源码
因为之前写过一个照片分类软件,用过一个简单的工具类:
在我的 GitHub 中有,地址:C# 工具组 —— EXIF 工具
目录:Fork/Azylee.Utils/Azylee.Core/IOUtils/ImageUtils/
ExifHelper.cs
ExifTagNames.cs
2、使用代码:
//一个简单的Exif信息读取工具类,根据需要转换数据类型
ExifHelper exif = new ExifHelper(filename);
foreach (ExifTagNames tag in (ExifTagNames[]) Enum.GetValues(typeof(ExifTagNames)))
{double _double = exif.GetPropertyDouble((int)tag);
string _string = exif.GetPropertyString((int)tag);
char _char = '=';// exif.GetPropertyChar((int)tag);
sb.AppendLine($"{tag.ToString()} : {_double} : {_string} : {_char}");
}
sb.AppendLine($"GpsAltitude: {exif.GetPropertyDouble((int)ExifTagNames.GpsAltitude)}");
sb.AppendLine($"GpsLatitude: {exif.GetPropertyDouble((int)ExifTagNames.GpsLatitude)}");
sb.AppendLine($"GpsLongitude: {exif.GetPropertyDouble((int)ExifTagNames.GpsLongitude)}");