public static void ChangeCaptureDate(string directory, LongTimeSpan timeDiff, string filter = ".jpg|.jpeg", string camerafilter = null, BackgroundWorker worker = null, DoWorkEventArgs e = null) { DirectoryInfo dirInfo = new DirectoryInfo(directory); FileInfo[] fileInfos = dirInfo.GetFiles(); int progress = 0; foreach (var fileInfo in fileInfos) { if (ApplyFilter(fileInfo.Name, filter)) { ChangeCaptureDateInFile(fileInfo.FullName, timeDiff, camerafilter); } progress++; if (worker != null) { if (worker.CancellationPending == true) { e.Cancel = true; return; } else { worker.ReportProgress(100 * progress / fileInfos.Length); } } } return; }
public static void ChangeCaptureDateInFile(string fullFilename, LongTimeSpan timeDiff, string camerafilter = null) { string capDate = ""; Encoding enc = Encoding.Default; DateTime datetime; Byte[] bytes = File.ReadAllBytes(fullFilename); using (MemoryStream stream = new MemoryStream(bytes)) using (Image img = Image.FromStream(stream)) { if (camerafilter != null) { ImageHeaderInfo info = GetHeaderInfo(img.PropertyItems); if (camerafilter != info.FullCameraName) { return; } } foreach (PropertyItem Info in img.PropertyItems) { switch (Info.Id.ToString("X")) { case "110": break; case "9003": capDate = enc.GetString(Info.Value, 0, Info.Len - 1); datetime = DateTime.ParseExact(capDate, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); datetime = timeDiff.IncreaseDateTime(datetime); List <byte> zero = new List <byte>(new byte[] { 0 }); byte[] newValue = enc.GetBytes(datetime.ToString("yyyy:MM:dd HH:mm:ss")).Concat(zero).ToArray(); Info.Value = newValue; Info.Len = newValue.Length; img.SetPropertyItem(Info); break; } } img.Save(fullFilename); } }