如何判断网站被google k,佛山网站代运营,百度应用商店app下载安装,免费手机网站源码下载摄影工作室通常会有大量的图片素材#xff0c;在进行图片整理和分类时#xff0c;需要知道每张图片的尺寸、分辨率、GPS 经纬度#xff08;如果拍摄时记录了#xff09;等信息#xff0c;以便更好地管理图片资源#xff0c;比如根据图片尺寸和分辨率决定哪些图片适合用于…摄影工作室通常会有大量的图片素材在进行图片整理和分类时需要知道每张图片的尺寸、分辨率、GPS 经纬度如果拍摄时记录了等信息以便更好地管理图片资源比如根据图片尺寸和分辨率决定哪些图片适合用于大型海报哪些适合用于网页展示。将这些信息导出表格后可以方便地进行筛选、排序和统计。在地理信息相关的研究中可能会收集大量带有 GPS 经纬度信息的图片通过提取这些图片的属性信息并导出表格可以对图片的分布区域、拍摄海拔等进行分析从而辅助地理信息的研究和绘制地图等工作。 详细步骤
1. 创建 WPF 项目
首先打开 Visual Studio创建一个新的 WPF 应用程序项目。
2. 设计界面
在 MainWindow.xaml 文件中设计界面添加必要的控件如按钮用于选择图片文件夹文本框用于显示文件夹路径以及一个按钮用于导出表格。以下是一个简单的示例
xml
Window x:ClassImageMetadataExtractor.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleImage Metadata Extractor Height350 Width525GridButton Content选择图片文件夹 HorizontalAlignmentLeft Margin20,20,0,0 VerticalAlignmentTop Width150 ClickSelectFolderButton_Click/TextBox x:NameFolderPathTextBox HorizontalAlignmentLeft Height23 Margin180,20,0,0 TextWrappingWrap VerticalAlignmentTop Width300 IsReadOnlyTrue/Button Content导出表格 HorizontalAlignmentLeft Margin20,60,0,0 VerticalAlignmentTop Width150 ClickExportTableButton_Click//Grid
/Window3. 实现选择文件夹功能
在 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 ListDictionarystring, object GetImageMetadata(string folderPath)
{ListDictionarystring, object metadataList new ListDictionarystring, 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.OfTypeGpsDirectory().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 Liststring { 文件名, 宽度, 高度, 分辨率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 表格中。
通过以上步骤你可以实现批量获取图片属性信息并导出表格的功能。