我有一个分析图像的应用程序。我得收回一张照片的日期。我使用这个函数:
var r = new Regex(":");
var myImage = LoadImageNoLock(path);
{
PropertyItem propItem = null;
try
{
propItem = myImage.GetPropertyItem(36867);
}
catch{
try
{
propItem = myImage.GetPropertyItem(306);
}
catch { }
}
if (propItem != null)
{
var dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
return DateTime.Parse(dateTaken);
}
else
{
return null;
}
}
我的申请与相机拍摄的照片配合得很好。但是现在,我从这样的网络摄像头中保存照片:
private void Webcam_PhotoTakenEvent(Bitmap inImage)
{
// Save photo on disk
if (_takePhoto == true)
{
// Save the photo on disk
inImage.Save(_currentPath + "/BV_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".jpeg");
}
}
在这种情况下,我以前的函数无法工作,因为图像文件不包含任何PropertyItem。
当我们手动保存映像时,有什么方法可以撤回PropertyItem所取的日期吗?
提前谢谢。
发布于 2017-07-18 18:39:42
最后,我找到了一个解决方案与亚历克斯的评论。
我手动设置了PropertyItem:
private void Webcam_PhotoTakenEvent(Bitmap inImage)
{
// Set the Date Taken in the EXIF Metadata
var newItem = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
newItem.Id = 36867; // Taken date
newItem.Type = 2;
// The format is important the decode the date correctly in the futur
newItem.Value = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss") + "\0");
newItem.Len = newItem.Value.Length;
inImage.SetPropertyItem(newItem);
// Save the photo on disk
inImage.Save(_currentPath + "/BV_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".jpeg");
}
https://stackoverflow.com/questions/45166897
复制相似问题