public static BitmapMetadata ReadMetadata(string sourceFile) { BitmapMetadata sourceMetadata = null; BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile; // Open the File using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read)) { // Decode the file and cache the content onload (BitmapCacheOption.OnLoad) // If you don't do this sourceMetadata won't be fully loaded BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.OnLoad); // Check source has valid frames if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null) { // Clone the metadata so we can throw away the reference to the underlying file sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata; } else { throw new Exception("Unable to read Metadata from File"); } } return sourceMetadata; } |