public MetadataPortal(ImageInfo imageInfo) { fImageInfo = imageInfo; BitmapMetadata md = imageInfo.GetSystemImageSource().Metadata as BitmapMetadata; fNewMetadata = md != null?md.Clone() : new BitmapMetadata(imageInfo.GetImageFormatDescription()); }
/// <summary> /// Detect whether the metadata object is valid and can be copied successfully /// </summary> /// <remarks> /// ImageMetadata.Clone() causes an exception if there is something wrong with the metadata object. /// Operation is rather expensive. /// </remarks> /// <param name="metadata">Metadata object to be checked</param> /// <returns>true if valid, false if invalid</returns> public static bool IsMetadataObjectValid(this BitmapMetadata metadata) { try { _ = metadata.Clone(); return(true); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception) #pragma warning restore CA1031 // Do not catch general exception types { return(false); } }
private static BitmapMetadata GetPersistedWICMetadata(BitmapMetadata original) { BitmapMetadata newMetadata = null; string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { BitmapSource source = BitmapSource.Create(1, 1, 96.0, 96.0, System.Windows.Media.PixelFormats.Gray8, null, new byte[] { 255 }, 1); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source, null, WICMetadataHelper.ConvertSaveMetaDataFormat(original, encoder), null)); using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write)) { encoder.Save(stream); } using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { BitmapFrame frame = BitmapFrame.Create(stream); try { BitmapMetadata temp = frame.Metadata as BitmapMetadata; newMetadata = temp?.Clone(); } catch (NotSupportedException) { } } } finally { File.Delete(path); } return(newMetadata); }
/// <summary> /// Sets the image orientation to indicate the origin is the top left corner. /// </summary> /// <param name="metadata">The meta data.</param> /// <returns>The modified meta data.</returns> /// <exception cref="ArgumentNullException"><paramref name="metadata"/> is null.</exception> internal static BitmapMetadata SetOrientationToTopLeft(BitmapMetadata metadata) { if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); } string format = string.Empty; try { format = metadata.Format; // Some WIC codecs do not implement the format property. } catch (ArgumentException) { } catch (NotSupportedException) { } BitmapMetadata newMetadata = metadata.Clone(); try { if (format == "jpg") { if (metadata.ContainsQuery("/app1/ifd")) { newMetadata.SetQuery("/app1/ifd/{ushort=274}", (ushort)ImageOrientation.TopLeft); } if (metadata.ContainsQuery("/xmp")) { newMetadata.SetQuery("/xmp/tiff:Orientation", ((ushort)ImageOrientation.TopLeft).ToString(CultureInfo.InvariantCulture)); } } else if (format == "png") { BitmapMetadata xmp = GetXMPMetadata(metadata, format); if (xmp != null) { xmp.SetQuery("/tiff:Orientation", ((ushort)ImageOrientation.TopLeft).ToString(CultureInfo.InvariantCulture)); if (string.Equals(metadata.GetQuery("/iTXt/Keyword") as string, "XML:com.adobe.xmp", StringComparison.Ordinal)) { byte[] packet = ExtractXMPPacket(xmp); if (packet != null) { newMetadata.SetQuery("/iTXt/TextEntry", Encoding.UTF8.GetString(packet)); } } } } else if (format != "gif") { if (metadata.ContainsQuery("/ifd")) { newMetadata.SetQuery("/ifd/{ushort=274}", (ushort)ImageOrientation.TopLeft); if (metadata.ContainsQuery("/ifd/xmp")) { newMetadata.SetQuery("/ifd/xmp/tiff:Orientation", ((ushort)ImageOrientation.TopLeft).ToString(CultureInfo.InvariantCulture)); } } } } catch (IOException) { // WINCODEC_ERR_INVALIDQUERYREQUEST } return(newMetadata); }
public void Execute() { string path; using (var inputStream = File.OpenRead(_file)) { var decoder = BitmapDecoder.Create( inputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); var encoder = BitmapEncoder.Create(decoder.CodecInfo.ContainerFormat); if (!encoder.CanEncode()) { encoder = BitmapEncoder.Create(_settings.FallbackEncoder); } ConfigureEncoder(encoder); if (decoder.Metadata != null) { try { encoder.Metadata = decoder.Metadata; } catch (InvalidOperationException) { } } if (decoder.Palette != null) { encoder.Palette = decoder.Palette; } foreach (var originalFrame in decoder.Frames) { BitmapMetadata metadata = (BitmapMetadata)originalFrame.Metadata; if (metadata != null) { try { // Detect whether metadata can copied successfully _ = metadata.Clone(); } catch (ArgumentException) { metadata = null; } } encoder.Frames.Add( BitmapFrame.Create( Transform(originalFrame), thumbnail: null, metadata, // TODO: Add an option to strip any metadata that doesn't affect rendering (issue #3) colorContexts: null)); } path = GetDestinationPath(encoder); Directory.CreateDirectory(Path.GetDirectoryName(path)); using (var outputStream = File.Open(path, FileMode.CreateNew, FileAccess.Write)) { encoder.Save(outputStream); } } if (_settings.KeepDateModified) { File.SetLastWriteTimeUtc(path, File.GetLastWriteTimeUtc(_file)); } if (_settings.Replace) { var backup = GetBackupPath(); File.Replace(path, _file, backup, ignoreMetadataErrors: true); FileSystem.DeleteFile(backup, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin); } }