
C#中获取照片时间的代码
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本文介绍了如何在C#编程语言中编写代码以读取和提取照片的时间信息(如拍摄日期和时间),为开发者提供了一种有效管理图片元数据的方法。
C#获取照片时间的源码可以用于读取图片中的EXIF数据来提取拍摄日期和其他相关信息。下面是一个简单的示例代码:
```csharp
using System;
using System.Drawing;
using System.IO;
public class PhotoTimeGetter {
public static void Main() {
string filePath = path/to/your/image.jpg;
try {
Image photo = Image.FromFile(filePath);
// 获取EXIF属性集合
PropertyItem propItem = photo.GetPropertyItem(36867); // EXIF标签ID为36867对应DateTime字段
string dateTimeValue = System.Text.Encoding.UTF8.GetString(propItem.Value);
Console.WriteLine(拍摄日期: + dateTimeValue);
}
catch (FileNotFoundException) {
Console.WriteLine(文件未找到,请确保路径正确。);
}
catch (ArgumentException e) {
if(e.Message.Contains(not supported))
Console.WriteLine(此图片不支持EXIF数据或指定的属性ID无效。);
throw;
}
}
}
```
这段代码首先尝试从给定的照片文件中加载图像,然后读取其中包含的EXIF元数据以获取拍摄日期,并将其打印出来。如果遇到错误(例如,当提供的路径不存在或者图片不支持EXIF时),程序将输出相应的提示信息。
以上就是使用C#语言来检索照片时间的基本方法和示例代码实现。
全部评论 (0)


