private static void ReadMetadataUsingWIC(BitmapMetadata metadata) { string comment = WICMetadataHelper.GetEXIFComment(metadata); if (string.IsNullOrEmpty(comment)) { Console.WriteLine("EXIF comment not found"); } else { Console.WriteLine("EXIF comment: " + comment); } string xmpDesc = WICMetadataHelper.GetXMPDescription(metadata); if (string.IsNullOrEmpty(xmpDesc)) { Console.WriteLine("XMP description not found"); } else { Console.WriteLine("XMP description: " + xmpDesc); } Console.WriteLine(); }
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); }
private static void Main(string[] args) { CommandLineParser parser = new CommandLineParser(args); if (parser.ShowHelp) { Console.WriteLine("Usage: WICMetadataDemo.exe [-dump] [-help] [image]."); } else { string imagePath = parser.ImageFileName; if (string.IsNullOrWhiteSpace(imagePath)) { imagePath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "GreenSquare.jpg"); } try { BitmapFrame frame = BitmapFrame.Create(new Uri(imagePath)); BitmapMetadata metadata = null; try { metadata = frame.Metadata as BitmapMetadata; } catch (NotSupportedException) { } if (metadata != null) { string format = string.Empty; try { format = metadata.Format; // Some WIC codecs do not implement the format property. } catch (ArgumentException) { } catch (NotSupportedException) { } if (parser.DumpMetadata) { Console.WriteLine("{0} format WIC metadata\n", string.IsNullOrEmpty(format) ? "Unspecified" : format.ToUpperInvariant()); WICMetadataHelper.DumpToStdOut(metadata); Console.WriteLine(); } else { TestWICMetadataPersistance(metadata); TestGDIPlusMetadataPersistance(imagePath); } } else { Console.WriteLine("The image does not contain any WIC metadata.\n"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } Console.WriteLine("Press any key to exit."); Console.ReadLine(); }