Question

I am trying to extract time information from a tiff file using metadata class; Here is the part where time information is in the tiff file:

<Plane TheZ="0" TheT="0" TheC="0" DeltaT="0.2345"/>

where 0.2345 is the information I am trying to extract. And here is my code that tried to get it out:

string searchtext = "DeltaT=";
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            TiffBitmapDecoder tbd = new TiffBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

            if (tbd.Frames[0] != null && tbd.Frames[0].Metadata != null)
            {
                //BitmapMetadata bmd = new BitmapMetadata("tiff");

                BitmapMetadata bmd = tbd.Frames[0].Metadata as BitmapMetadata;

                bmd.GetQuery(@searchText);              

            }

However, the line bmd.GetQuery(@searchText); threw an exception "Metadata query request is not valid"; I am not sure how to change it to make it get the 0.2345 value. Anyone has any idea? Thanks alot.

Here is the tiff file so that you can take a look: http://dl.dropbox.com/u/105139407/ChanA_0001_0001_0001_0003.tif

Was it helpful?

Solution

Apparently the XML you're looking for is in "/ifd/{ushort=270}" (don't ask me why - I don't know anything about how TIFF metadata is supposed to look like...). Note that it returns the whole XML document, so you still have to parse it. This code retrieves the value of DeltaT:

var decoder = new TiffBitmapDecoder(new Uri(fileName), BitmapCreateOptions.None, BitmapCacheOption.Default);
var metadata = (BitmapMetadata)decoder.Frames[0].Metadata;
string xml = (string)metadata.GetQuery("/ifd/{ushort=270}");
var doc = XDocument.Parse(xml);
var ns = doc.Root.GetDefaultNamespace();
var plane = doc.Root.Element(ns + "Image")
                    .Element(ns + "Pixels")
                    .Element(ns + "Plane");
double deltaT = (double)plane.Attribute("DeltaT");

EDIT: here's a LINQPad script I use to have a quick look at the metadata of an image: http://pastebin.com/daBTdW33. Feel free to use it or adapt it to your needs ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top