tomlev

Image metadata

Feb 17th, 2013
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LINQPad script
  2. // References: System.Xaml, WindowsBase, PresentationCore
  3. // Namespaces: System.Windows.Media.Imaging
  4.  
  5. void Main()
  6. {
  7.     string path = @"(image_path)";
  8.     Uri imageUri = new Uri(path);
  9.    
  10.     var decoder = BitmapDecoder.Create(imageUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
  11.    
  12.     // Global metadata
  13.     DumpMetadata(decoder.Metadata);
  14.    
  15.     // Metadata for each frame
  16.     DumpAllMetadata(decoder.Frames);
  17. }
  18.  
  19. void DumpAllMetadata(IEnumerable<BitmapFrame> frames)
  20. {
  21.     int i = 0;
  22.     foreach (var f in frames)
  23.     {
  24.         using (var writer = new StringWriter())
  25.         {
  26.             DumpMetadata((BitmapMetadata)f.Metadata, writer);
  27.             writer.ToString().Dump("Frame " + i++);
  28.         }
  29.     }
  30. }
  31.  
  32. void DumpMetadata(BitmapMetadata metadata)
  33. {
  34.     DumpMetadata(metadata, 0, Console.Out);
  35. }
  36.  
  37. void DumpMetadata(BitmapMetadata metadata, TextWriter writer)
  38. {
  39.     DumpMetadata(metadata, 0, writer);
  40. }
  41.  
  42. void DumpMetadata(BitmapMetadata metadata, int indentLevel)
  43. {
  44.     DumpMetadata(metadata, indentLevel, Console.Out);
  45. }
  46.  
  47. void DumpMetadata(BitmapMetadata metadata, int indentLevel, TextWriter writer)
  48. {
  49.     if (metadata == null)
  50.     {
  51.         writer.WriteLine ("(No metadata)");
  52.         return;
  53.     }
  54.     string indent = new string('\t', indentLevel);
  55.     foreach (var propertyName in metadata)
  56.     {
  57.         object value = metadata.GetQuery(propertyName);
  58.         BitmapMetadata complexProperty = value as BitmapMetadata;
  59.         if (complexProperty != null)
  60.         {
  61.             writer.WriteLine("{0}{1} =", indent, propertyName);
  62.             DumpMetadata(complexProperty, indentLevel + 1, writer);
  63.         }
  64.         else
  65.         {
  66.             Type type = null;
  67.             if (value != null)
  68.                 type = value.GetType();
  69.             writer.WriteLine("{0}{1} = {2} ({3})", indent, propertyName, value, type);
  70.         }
  71.     }
  72. }
RAW Paste Data