Blog | Photography | Highlights | Contact | About | Atom & RSS Feeds
Using WIC & C# to read Windows Live Photo Gallery’s People Tags
  • Blog
  • Using WIC & C# to read Windows Live Photo Gallery’s People Tags

In my last posting I talked about how Windows Live Photo Gallery (WLPG) stores People tags in XMP. In this post I’m going to extend the Windows Imaging Component code from Robert Wlodarczyk’s Blog to read the data. The code is pretty simple to use, just make sure you have PresentationCore registered in your project and the System.Windows.Media.Imaging namespace.  
public void ReadWLPGRegions(string sourceFile)
{
   // Declare a bunch of XMP paths (see my last blog for details)
   string microsoftRegions = @"/xmp/MP:RegionInfo/MPRI:Regions";
   string microsoftPersonDisplayName = @"/MPReg:PersonDisplayName";
   string microsoftRectangle = @"/MPReg:Rectangle";
   BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
   using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
   {
      BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None);
      // Check source has valid frames
      if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
      {
         BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata as BitmapMetadata;
         // Check there is a RegionInfo
         if (sourceMetadata.ContainsQuery(microsoftRegions))
         {
            BitmapMetadata regionsMetadata = sourceMetadata.GetQuery(microsoftRegions) as BitmapMetadata;
            // Loop through each Region
            foreach (string regionQuery in regionsMetadata)
            {
               string regionFullQuery = microsoftRegions + regionQuery;
               // Query for all the data for this region
               BitmapMetadata regionMetadata = sourceMetadata.GetQuery(regionFullQuery) as BitmapMetadata;
               if (regionMetadata != null)
               {
                  if (regionMetadata.ContainsQuery(microsoftPersonDisplayName))
                  {
                     Console.WriteLine("PersonDisplayName:\t"
                        + regionMetadata.GetQuery(WpfProperties.MicrosoftPersonDisplayName).ToString());
                  }
                  if (regionMetadata.ContainsQuery(microsoftRectangle))
                  {
                     Console.WriteLine("Rectangle:\t\t"
                        + regionMetadata.GetQuery(WpfProperties.MicrosoftRectangle).ToString());
                  }
               }
            }
         }
      }
   }
}
If you have people tagged in your photos you should get some output similar to this:   image Pretty easy when you know how! In my next posting I’ll show you what I’ve done to use this data and integrate it to tassography.com.

This website, all photography & other content is Copyright © Ben Vincent. Unauthorised use of images is strictly prohibited.
Last Updated: Wed, 14 Dec 2011, 16:30:58    |    Website Version v4.0.4138.41239    |    Content v7.002