/// <summary> /// Shows only IPTC directory and only the TAG_HEADLINE value for one file. /// </summary> /// <param name="aFileName">the image file name (ex: c:/temp/a.jpg)</param> /// <returns>The information about IPTC for this image but only the TAG_HEADLINE tag as a string</returns> public static string ShowOneFileOnlyIptcOnlyTagTAG_HEADLINE(string aFileName) { Metadata lcMetadata = null; try { FileInfo lcImgFile = new FileInfo(aFileName); // Loading all meta data lcMetadata = JpegMetadataReader.ReadMetadata(lcImgFile); } catch (JpegProcessingException e) { Console.Error.WriteLine(e.Message); return("Error"); } // Now try to print them StringBuilder lcBuff = new StringBuilder(1024); lcBuff.Append("---> ").Append(aFileName).Append(" <---").AppendLine(); // We want anly IPCT directory IptcDirectory lcIptDirectory = (IptcDirectory)lcMetadata.GetDirectory("com.drew.metadata.iptc.IptcDirectory"); if (lcIptDirectory == null) { lcBuff.Append("No Iptc for this image.!").AppendLine(); return(lcBuff.ToString()); } // We look for potential error if (lcIptDirectory.HasError) { Console.Error.WriteLine("Some errors were found, activate trace using /d:TRACE option with the compiler"); } // Then we want only the TAG_HEADLINE tag if (!lcIptDirectory.ContainsTag(IptcDirectory.TAG_HEADLINE)) { lcBuff.Append("No TAG_HEADLINE for this image.!").AppendLine(); return(lcBuff.ToString()); } string lcTagDescription = null; try { lcTagDescription = lcIptDirectory.GetDescription(IptcDirectory.TAG_HEADLINE); } catch (MetadataException e) { Console.Error.WriteLine(e.Message); } string lcTagName = lcIptDirectory.GetTagName(IptcDirectory.TAG_HEADLINE); lcBuff.Append(lcTagName).Append('=').Append(lcTagDescription).AppendLine(); return(lcBuff.ToString()); }
/// <summary> /// Gets and caches the most relevant date available for an image. /// It looks first at the EXIF date, then at the creation date from ITPC data, /// and then at the file's creation date if everything else failed. /// </summary> /// <param name="pictureFileInfo">The FileInfo for the image.</param> /// <returns>The date.</returns> internal static DateTime GetImageDate(FileInfo pictureFileInfo) { string cacheKey = "date(" + pictureFileInfo.FullName + ")"; Cache cache = HttpContext.Current.Cache; DateTime result = DateTime.MinValue; object cached = cache[cacheKey]; if (cached == null) { Metadata data = GetImageData(pictureFileInfo); ExifDirectory directory = (ExifDirectory)data.GetDirectory(typeof(ExifDirectory)); if (directory.ContainsTag(ExifDirectory.TAG_DATETIME)) { try { result = directory.GetDate(ExifDirectory.TAG_DATETIME); } catch { } } else { IptcDirectory iptcDir = (IptcDirectory)data.GetDirectory(typeof(IptcDirectory)); if (iptcDir.ContainsTag(IptcDirectory.TAG_DATE_CREATED)) { try { result = iptcDir.GetDate(IptcDirectory.TAG_DATE_CREATED); } catch { } } else { result = pictureFileInfo.CreationTime; } } cache.Insert(cacheKey, result, new CacheDependency(pictureFileInfo.FullName)); } else { result = (DateTime)cached; } return(result); }