摄影工作室通常会有大量的图片素材,在进行图片整理和分类时,需要知道每张图片的尺寸、分辨率、GPS 经纬度(如果拍摄时记录了)等信息,以便更好地管理图片资源,比如根据图片尺寸和分辨率决定哪些图片适合用于大型海报,哪些适合用于网页展示。将这些信息导出表格后,可以方便地进行筛选、排序和统计。在地理信息相关的研究中,可能会收集大量带有 GPS 经纬度信息的图片,通过提取这些图片的属性信息并导出表格,可以对图片的分布区域、拍摄海拔等进行分析,从而辅助地理信息的研究和绘制地图等工作。
详细步骤
1. 创建 WPF 项目
首先,打开 Visual Studio,创建一个新的 WPF 应用程序项目。
2. 设计界面
在 MainWindow.xaml
文件中设计界面,添加必要的控件,如按钮用于选择图片文件夹,文本框用于显示文件夹路径,以及一个按钮用于导出表格。以下是一个简单的示例:
xml
<Window x:Class="ImageMetadataExtractor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Image Metadata Extractor" Height="350" Width="525">
<Grid>
<Button Content="选择图片文件夹" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="150" Click="SelectFolderButton_Click"/>
<TextBox x:Name="FolderPathTextBox" HorizontalAlignment="Left" Height="23" Margin="180,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" IsReadOnly="True"/>
<Button Content="导出表格" HorizontalAlignment="Left" Margin="20,60,0,0" VerticalAlignment="Top" Width="150" Click="ExportTableButton_Click"/>
</Grid>
</Window>
3. 实现选择文件夹功能
在 MainWindow.xaml.cs
文件中,实现选择文件夹的功能。需要使用 System.Windows.Forms.FolderBrowserDialog
来选择文件夹,并将选择的文件夹路径显示在文本框中。
csharp
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
namespace ImageMetadataExtractor
{
public partial class MainWindow : Window
{
private string selectedFolderPath;
public MainWindow()
{
InitializeComponent();
}
private void SelectFolderButton_Click(object sender, RoutedEventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
selectedFolderPath = folderBrowserDialog.SelectedPath;
FolderPathTextBox.Text = selectedFolderPath;
}
}
}
}
}
4. 批量获取图片属性信息
使用 System.Drawing
和 MetadataExtractor
库来获取图片的属性信息。MetadataExtractor
是一个强大的用于提取图片元数据的库,可以通过 NuGet 包管理器进行安装。以下是获取图片属性信息的代码:
csharp
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
using System.Collections.Generic;
using System.Drawing;
private List<Dictionary<string, object>> GetImageMetadata(string folderPath)
{
List<Dictionary<string, object>> metadataList = new List<Dictionary<string, object>>();
string[] imageFiles = Directory.GetFiles(folderPath, "*.jpg;*.jpeg;*.png", SearchOption.AllDirectories);
foreach (string imageFile in imageFiles)
{
metadata["文件名"] = Path.GetFileName(imageFile);
try
{
using (Image image = Image.FromFile(imageFile))
{
metadata["宽度"] = image.Width;
metadata["高度"] = image.Height;
metadata["分辨率X"] = image.HorizontalResolution;
metadata["分辨率Y"] = image.VerticalResolution;
metadata["位深度"] = image.PixelFormat.ToString();
}
var directories = ImageMetadataReader.ReadMetadata(imageFile);
var gpsDirectory = directories.OfType<GpsDirectory>().FirstOrDefault();
if (gpsDirectory != null)
{
if (gpsDirectory.ContainsTag(GpsDirectoryBase.TagLatitude) && gpsDirectory.ContainsTag(GpsDirectoryBase.TagLongitude))
{
var latitude = gpsDirectory.GetGeoLocation().Latitude;
var longitude = gpsDirectory.GetGeoLocation().Longitude;
metadata["GPS纬度"] = latitude;
metadata["GPS经度"] = longitude;
}
if (gpsDirectory.ContainsTag(GpsDirectoryBase.TagAltitude))
{
metadata["海拔"] = gpsDirectory.GetDouble(GpsDirectoryBase.TagAltitude);
}
}
// 图片面积(简单计算为宽度 * 高度)
metadata["面积"] = (int)metadata["宽度"] * (int)metadata["高度"];
}
catch (Exception ex)
{
metadata["错误信息"] = ex.Message;
}
metadataList.Add(metadata);
}
return metadataList;
}
5. 导出表格
使用 Microsoft.Office.Interop.Excel
库将获取到的图片属性信息导出到 Excel 表格中。同样,可以通过 NuGet 包管理器安装相关依赖。以下是导出表格的代码:
csharp
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
private void ExportTableButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(selectedFolderPath))
{
MessageBox.Show("请先选择图片文件夹!");
return;
}
var metadataList = GetImageMetadata(selectedFolderPath);
var excelApp = new Application();
var workbook = excelApp.Workbooks.Add();
var worksheet = workbook.ActiveSheet;
// 写入表头
var headers = new List<string> { "文件名", "宽度", "高度", "分辨率X", "分辨率Y", "位深度", "GPS纬度", "GPS经度", "海拔", "面积", "错误信息" };
for (int i = 0; i < headers.Count; i++)
{
worksheet.Cells[1, i + 1] = headers[i];
}
// 写入数据
for (int i = 0; i < metadataList.Count; i++)
{
var metadata = metadataList[i];
for (int j = 0; j < headers.Count; j++)
{
if (metadata.ContainsKey(headers[j]))
{
worksheet.Cells[i + 2, j + 1] = metadata[headers[j]];
}
}
}
// 保存文件
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx";
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveFileDialog.FileName);
MessageBox.Show("表格导出成功!");
}
// 释放资源
workbook.Close();
excelApp.Quit();
Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(excelApp);
}
6. 运行程序
编译并运行程序,点击 "选择图片文件夹" 按钮选择包含图片的文件夹,然后点击 "导出表格" 按钮,选择保存路径,即可将图片属性信息导出到 Excel 表格中。
通过以上步骤,你可以实现批量获取图片属性信息并导出表格的功能。