【问题标题】:C# Set / create DateTaken parameterC# 设置/创建 DateTaken 参数
【发布时间】:2020-02-15 09:43:36
【问题描述】:

我想用 C# 设置 DateTaken 参数,因为我有很多没有这个日期的照片。 我只找到了这条评论changing-datetaken-of-a-photo 在这个主题中,他们正在更改它而不是创建它。

如果使用该函数,但DataTakenProperty1或DataTakenProperty2为null,不能设置。

    private static void SetDateTaken(string path, DateTime NEWdate)
    {
        Image theImage = new Bitmap(path);
        PropertyItem[] propItems = theImage.PropertyItems;
        Encoding _Encoding = Encoding.UTF8;

        var DataTakenPropert = propItems.SetValue(NEWdate.ToString("yyyy:MM:dd HH:mm:ss"), ??How do i know the index??);

        theImage.SetPropertyItem(DataTakenProperty);
        string new_path = Path.GetDirectoryName(path) + "\\_" + Path.GetFileName(path);
        theImage.Save(new_path);
        theImage.Dispose();
    }

感谢您的帮助

【问题讨论】:

    标签: c# bitmap exif


    【解决方案1】:

    您可以使用一个小技巧来实现这一点,因为当 PropertyItem 为空时,您无法启动它。

    很难设置属性项,因为 PropertyItem 类 没有公共构造函数。解决此限制的一种方法是 通过检索 PropertyItems 属性来获取 PropertyItem 值或调用已经存在的 Image 的 GetPropertyItem 方法 有属性项目。然后你可以设置PropertyItem的字段 并将其传递给 SetPropertyItem。

    private static void SetDateTaken(string path, string samplePath, DateTime NEWdate)
    {         
        Encoding _Encoding = Encoding.UTF8;
        Image theImage = new Bitmap(path);
        PropertyItem[] propItems = theImage.PropertyItems;
    
        var DataTakenProperty1 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
        var DataTakenProperty2 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
    
        //// this is where you do the hack
        if (DataTakenProperty1 == null)
        {
            Image sampleImage = new Bitmap(samplePath);
            PropertyItem fakePropertyItem1 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
            fakePropertyItem1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
            fakePropertyItem1.Len = fakePropertyItem1.Value.Length;
            theImage.SetPropertyItem(fakePropertyItem1);
    
            PropertyItem fakePropertyItem2 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
            fakePropertyItem2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
            fakePropertyItem2.Len = fakePropertyItem2.Value.Length;
            theImage.SetPropertyItem(fakePropertyItem2);
        }
        else
        {
            DataTakenProperty1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
            DataTakenProperty2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
            theImage.SetPropertyItem(DataTakenProperty1);
            theImage.SetPropertyItem(DataTakenProperty2);
        }
    
        theImage.Save(newPath);
        theImage.Dispose();
    }
    

    List of PropertyIds

    【讨论】:

    • 您好,感谢您的帮助,我没有收到任何错误,但不幸的是它无法正常工作。我试过这个 ID(也有和没有 prevois 0x ):132、9003、9004。DateTaken 属性仍然是空的/资源管理器没有显示它
    • 您是否正确阅读了答案?上面的代码已经过测试,它也可以工作
    • 嗯,它不起作用,我添加了“theImage.Save(path); theImage.Dispose();”什么也没发生。 Ff我又读了一遍dei,然后让我显示ID,9004没有显示,只有[770,34675,20752,20753,20754]
    • 我能够重现您使用 png 类型的问题,但对于 jpg,上述修改后的代码可以正常工作。 SamplePath 是带有 DateTaken 属性的 jpg。
    • 是的,png 就是问题所在。用jpg它工作正常。谢谢你。我只有几张 png 图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多