更改"DateTaken"照片的 [英] Changing "DateTaken" of a photo

查看:160
本文介绍了更改"DateTaken"照片的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从美国回来,在编辑完所有照片后,我注意到相机使用的是以色列时区,而不是美国人.有7个小时的时差,所以对我来说是个大问题.我有175GB的照片,但是我只"关心约350张照片.我无法手动编辑其EXIF,因此我考虑使用C#.

I just came back from a trip to the US, and after editing all the photos, I noticed that the camera used the Israeli time zone, and not the american. There is a 7 hours time difference, so it's a big problem for me. I have 175GB of photos, but I care "only" about 350 photos. I can't edit their EXIF manually, so I thought about using C#.

这个想法是,它将读取每张照片的EXIF,获取时间,并在原始照片中将时间设置为减去7小时.我尝试使用Image类,但是它不起作用.我尝试使用bitmapMetadate,它起作用了!我设法节省了时间,做了七个小时,但我不知道如何保存.我该怎么做?谢谢!

The idea is that it will read each photo's EXIF, get the time, and set the time minus 7 hours in the original photo. I tried using the Image class, but it doesn't work. I tried using the bitmapMetadate, and it worked! I've managed to get the time and do minus seven hours, but I have no idea how to save it. How can I do it? Thanks!

    public static string PhotoToBeEdited(FileInfo f)
    {
        FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
        BitmapSource img = BitmapFrame.Create(fs);
        BitmapMetadata md = (BitmapMetadata)img.Metadata;
        string date = md.DateTaken;
        Console.WriteLine(date);
        DateTime dt= DateTime.Parse(date);
        date = dt.AddHours(-7).ToString();

        [...]

        return date;
    }

推荐答案

我找到的最简单的方法是使用描述的技术

The simplest way I've found is using technic described here and System.Drawing.Bitmap;

代码应如下所示:

  public void ChangeDateTaken(string path)
    {
        Image theImage = new Bitmap(path);
        PropertyItem[] propItems = theImage.PropertyItems;
        Encoding _Encoding = Encoding.UTF8;
        var DataTakenProperty1 = propItems.Where(a => a.Id.ToString("x") == "9004").FirstOrDefault();
        var DataTakenProperty2 = propItems.Where(a => a.Id.ToString("x") == "9003").FirstOrDefault();
        string originalDateString = _Encoding.GetString(DataTakenProperty1.Value);
        originalDateString = originalDateString.Remove(originalDateString.Length - 1);
        DateTime originalDate = DateTime.ParseExact(originalDateString, "yyyy:MM:dd HH:mm:ss", null);

        originalDate = originalDate.AddHours(-7);


        DataTakenProperty1.Value = _Encoding.GetBytes(originalDate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
        DataTakenProperty2.Value = _Encoding.GetBytes(originalDate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
        theImage.SetPropertyItem(DataTakenProperty1);
        theImage.SetPropertyItem(DataTakenProperty2);
        string new_path = System.IO.Path.GetDirectoryName(path) + "\\_" + System.IO.Path.GetFileName(path);
        theImage.Save(new_path);
        theImage.Dispose();
    }

别忘了添加System.Drawing程序集. 另外,如果需要,您可能需要根据您的文化调整DateTime格式

Don't forget to add System.Drawing assembly. Also you will probably need to adjust DateTime format to your culture, if needed

这篇关于更改"DateTaken"照片的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆