private DateTime?getDateTimeOriginal(FileInfo imageFile) { ExifToolWrapper exifTool = new ExifToolWrapper(); ExifToolResponse jsonExifToolOutput = exifTool.execute(" -j -m -q -DateTimeOriginal", imageFile); return(jsonExifToolOutput.DateTimeOriginal); }
private void ExifToolShowImageMetadata() { // Clear the dictionary so we get fresh contents this.metadataDictionary.Clear(); // Start the exifTool process if its not already started if (this.exifTool == null) { this.exifTool = new ExifToolWrapper(); this.exifTool.Start(); } // Fetch the exif data using ExifTool Dictionary <string, string> exifDictionary = this.exifTool.FetchExifFrom(this.imageFilePath); // If there is no metadata, inform the user by setting bogus dictionary values which will appear on the grid if (exifDictionary.Count == 0) { this.metadataDictionary.Add("Empty", new Timelapse.Util.ImageMetadata("Empty", "No metadata found in the currently displayed image", "Navigate to a displayable image")); } // In order to populate the metadataDictionary and datagrid , we have to unpack the ExifTool dictionary, recreate the dictionary, and create a list containing four values List <Tuple <string, string, string, string> > metadataList = new List <Tuple <string, string, string, string> >(); foreach (KeyValuePair <string, string> metadata in exifDictionary) { this.metadataDictionary.Add(metadata.Key, new Timelapse.Util.ImageMetadata(String.Empty, metadata.Key, metadata.Value)); metadataList.Add(new Tuple <string, string, string, string>(metadata.Key, String.Empty, metadata.Key, metadata.Value)); } this.dataGrid.ItemsSource = metadataList; }
public void copyDateCreatedToDateTimeOriginal() { IXmpMeta xmpMeta; using (var fileStream = File.OpenRead(XmpFile.FullName)) { xmpMeta = XmpMetaFactory.Parse(fileStream); if (xmpMeta.DoesPropertyExist("http://ns.adobe.com/photoshop/1.0/", "photoshop:DateCreated")) { IXmpDateTime xmpMetaCreateDate = xmpMeta.GetPropertyDate("http://ns.adobe.com/photoshop/1.0/", "photoshop:DateCreated"); List <FileInfo> correspondingImageFiles = this.getCorrespondingImageFile(); foreach (FileInfo correspondingImageFile in correspondingImageFiles) { DateTime?DateTimeOriginal = this.getDateTimeOriginal(correspondingImageFile); if (DateTimeOriginal == null) { ExifToolWrapper exifTool = new ExifToolWrapper(); ExifToolResponse jsonExifToolOutput = exifTool.execute(string.Concat("-m -S -overwrite_original \"-DateTimeOriginal=", xmpMetaCreateDate.ToIso8601String(), "\""), correspondingImageFile); } else { Program.MainLogger.Information($"DateTimeOriginal TAG in file {correspondingImageFile} already set to {DateTimeOriginal}"); } } } else { Program.MainLogger.Information($"Cant't find 'photoshop:DateCreated' property in namespace 'http://ns.adobe.com/photoshop/1.0/'"); } } }
private void worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; string pathSortida = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SORTIDA"); Contador = 1; bool existeix = Directory.Exists(pathSortida); if (!existeix) { Directory.CreateDirectory(pathSortida); } //Buidar la carpeta DirectoryInfo di = new DirectoryInfo(pathSortida); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } using (ExifToolWrapper extw = new ExifToolWrapper()) { extw.Start(); string linea; // Append text to an existing file named "WriteLines.txt". string temporal = Path.Combine(Path.GetDirectoryName(RutaImatges), "index.txt"); string pathIndex = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SORTIDA"); using (StreamWriter outputFile = new StreamWriter(pathIndex + "\\index.txt", true)) { foreach (FotoModel foto in Fotos) { try { linea = foto.Client + foto.Expedient + foto.Arxiu_foto; outputFile.WriteLine(linea); d = extw.FetchExifFrom(_rutaImatges + "\\" + foto.Arxiu_foto); d.TryGetValue("Comment", out result); metadades = result.Split(separationString, System.StringSplitOptions.RemoveEmptyEntries); Utils.EscriureText(metadades, _rutaImatges + "\\" + foto.Arxiu_foto, foto.Arxiu_foto); Contador++; } catch (Exception ex) { continue; } } } } }
public ExIF(string FiletoScan, ref XMLParser raport) { Dictionary <string, string> d; using (var etw = new ExifToolWrapper(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\exiftool(-k).exe")) { etw.Start(); d = etw.FetchExifFrom(FiletoScan); if (!d.ContainsKey("Error")) { d.Remove("ExifTool Version Number"); d.Remove("File Name"); d.Remove("Directory"); d.Remove("File Size"); d.Remove("File Modification Date/Time"); d.Remove("File Access Date/Time"); d.Remove("File Creation Date/Time"); d.Remove("File Permissions"); raport.AddExIF(d); } } }
private void ReNameFile(FileInfo srcFileInfo, bool moveFile = true) { string file = srcFileInfo.FullName; ExifToolWrapper exif = new ExifToolWrapper(); exif.Run(file); string cameraModel = string.Empty; string cameraDateTime = string.Empty; ExifTagItem model = exif.Find("Camera Model Name"); if (string.IsNullOrEmpty(model.value)) { model = exif.Find("Model"); } cameraModel = model.value; ExifTagItem creationDate = exif.Find("Date/Time Original"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Creation Date"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Create Date"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Date Created"); } } } cameraDateTime = creationDate.value; if (string.IsNullOrEmpty(cameraModel)) { cameraModel = "未知"; } if (string.IsNullOrEmpty(cameraDateTime)) { return; } if (cameraDateTime.Contains("+08:00")) { cameraDateTime = cameraDateTime.Replace("+08:00", ""); } //文件名形式 照相机型号\年\月\日\年月日_时分秒_00 IFormatProvider ifp = new CultureInfo("zh-CN", true); DateTime dateTime = DateTime.ParseExact(cameraDateTime, "yyyy:MM:dd HH:mm:ss", ifp); string fileName = $"{cameraModel}\\{dateTime.Year}\\{dateTime.Month.ToString().PadLeft(2, '0')}\\{dateTime.Day.ToString().PadLeft(2, '0')}\\{dateTime.Year}{dateTime.Month.ToString().PadLeft(2, '0')}{dateTime.Day.ToString().PadLeft(2, '0')}_{dateTime.Hour.ToString().PadLeft(2, '0')}{dateTime.Minute.ToString().PadLeft(2, '0')}{dateTime.Second.ToString().PadLeft(2, '0')}_00"; string dstFile = Path.Combine(dstText.Text, fileName + srcFileInfo.Extension); FileInfo dstFileInfo = new FileInfo(dstFile); if (!dstFileInfo.Directory.Exists) { dstFileInfo.Directory.Create(); } if (!dstFileInfo.Exists) { if (moveFile) { File.Move(srcFileInfo.FullName, dstFileInfo.FullName); } else { File.Copy(srcFileInfo.FullName, dstFileInfo.FullName); } } else { //比较md5 string srcMd5 = md5File(srcFileInfo.FullName); string dstMd5 = md5File(dstFileInfo.FullName); if (srcMd5 == dstMd5) { if (moveFile) { srcFileInfo.Delete(); } return; } //文件添加后缀 int num = 0; while (true) { num++; dstFile = Path.Combine(dstText.Text, fileName + num.ToString().PadLeft(2, '0') + srcFileInfo.Extension); dstFileInfo = new FileInfo(dstFile); if (!dstFileInfo.Exists) { if (moveFile) { File.Move(srcFileInfo.FullName, dstFileInfo.FullName); } else { File.Copy(srcFileInfo.FullName, dstFileInfo.FullName); } return; } else { dstMd5 = md5File(dstFileInfo.FullName); if (srcMd5 == dstMd5) { if (moveFile) { srcFileInfo.Delete(); } return; } } } } }
// Use the ExifToolWrapper to load all the metadata // Note that this requires the exiftool(-k).exe to be available in the executables folder private void LoadExif() { extw = new ExifToolWrapper(); extw.Start(); dictMetaDataLookup = extw.FetchExifFrom(System.IO.Path.Combine (folderPath, fileName)); this.dg.ItemsSource = dictMetaDataLookup; // Bind the dictionary to the data grid. For some reason, I couldn't do this in xaml }
private async Task <IActionResult> AnalyzeFile(IFormFile ffile, String filePath, String thmFilePath, PhotoViewModelEx updrst, String usrName) { Boolean bThumbnailCreated = false; using (var fileStream = new FileStream(filePath, FileMode.Create)) { await ffile.CopyToAsync(fileStream); try { ExifToolWrapper wrap = new ExifToolWrapper(); wrap.Run(filePath); foreach (var item in wrap) { #if DEBUG System.Diagnostics.Debug.WriteLine("{0}, {1}, {2}", item.group, item.name, item.value); #endif if (item.group == "EXIF" || item.group == "Composite" || item.group == "XMP") { updrst.ExifTags.Add(item); } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif _logger.LogError(exp.Message); } try { using (MagickImage image = new MagickImage(filePath)) { updrst.Width = image.Width; updrst.Height = image.Height; // Retrieve the exif information ExifProfile profile = image.GetExifProfile(); if (profile != null) { using (MagickImage thumbnail = profile.CreateThumbnail()) { // Check if exif profile contains thumbnail and save it if (thumbnail != null) { thumbnail.Write(thmFilePath); updrst.ThumbWidth = thumbnail.Width; updrst.ThumbHeight = thumbnail.Height; bThumbnailCreated = true; } } } if (!bThumbnailCreated) { MagickGeometry size = new MagickGeometry(256, 256); // This will resize the image to a fixed size without maintaining the aspect ratio. // Normally an image will be resized to fit inside the specified size. size.IgnoreAspectRatio = false; image.Resize(size); updrst.ThumbWidth = image.Width; updrst.ThumbHeight = image.Height; // Save the result image.Write(thmFilePath); } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif _logger.LogError(exp.Message); updrst.success = false; updrst.error = exp.Message; } } updrst.OrgFileName = ffile.FileName; updrst.UploadedTime = DateTime.Now; updrst.IsOrgThumbnail = bThumbnailCreated; updrst.UploadedBy = usrName; return(Json(true)); }