Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Accessing WMF metadata with C#

, 21 Jun 2006
An article on how to use C# and the Windows Media Format SDK to read metadata in Windows Media Format files.
metadatareader_demo.zip
WMFMetadataReader
WMFMetadataReader
bin
obj
Debug
Refactor
TempPE
WMFMetadataReader.exe
Properties
Settings.settings
WMFSDKWrapper
bin
Debug
WMFSDKWrapper.dll
obj
Debug
Refactor
TempPE
Properties
metadatareader_src.zip
using System;
using System.Collections.Generic;
using System.Text;
using WMFSDKWrapper;    //managed wrapper to WMF SDK - provides access to metadata

namespace WMFMetadataReader
{
    /// This class contains the functionality for handling interaction with the media file
    /// metadata, via the WMF SDK managed wrapper class.
    public class MetaDataReader
    {

        /// Default constructor
        public MetaDataReader()
        {
        }

        /// Method to obtain a metadata attribute by passing in its name. 
        /// Assumes the metadata type is STRING.
        /// Uses the SDK function GetAttributeByName.
        ///
        /// param name="filename" - the filename (including path) of media file to interrogate
        /// param name="attrName" - the name of the field we're looking for
        /// returns - the value of the named attribute, empty string if not found, or error message
        public string GetFieldByName(string fileName, string attrName)
        {
            try
            {
                IWMMetadataEditor MetadataEditor;   //object used to access WMF file 
                IWMHeaderInfo3 HeaderInfo3;         //object to use access metadata 
                ushort streamNum = 0;               //media stream to interrogate
                WMT_ATTR_DATATYPE wAttribType;      //data type of attribute
                byte[] pbAttribValue = null;        //value of attribute (as returned by method call)
                ushort wAttribValueLen = 0;         //length of attribute (byte array)

                WMFSDKFunctions.WMCreateEditor(out MetadataEditor);

                MetadataEditor.Open(fileName);

                HeaderInfo3 = (IWMHeaderInfo3)MetadataEditor;

                //make call to get attribute length
                HeaderInfo3.GetAttributeByName(ref streamNum, attrName, out wAttribType, pbAttribValue, ref wAttribValueLen);
                //set byte array length
                pbAttribValue = new byte[wAttribValueLen];
                //make call again, which will get value into correct-length byte array
                HeaderInfo3.GetAttributeByName(ref streamNum, attrName, out wAttribType, pbAttribValue, ref wAttribValueLen);

                MetadataEditor.Close();

                return ConvertAttrToString(pbAttribValue, wAttribValueLen);
            }
            catch (Exception e)
            {
                return "ERROR: " + e.Message;
            }
        }//end method

        /// Method to convert byte array value into string. 
        /// (From the Microsoft WMF SDK sample.)
        ///
        /// param name="pbValue" - byte array value of attribute
        /// param name="dwValueLen" - Length of byte array
        private string ConvertAttrToString(byte[] pbValue, ushort dwValueLen)
        {
            string Value = "";

            if (0 == dwValueLen)
            {
                Value = "";
            }
            else
            {
                if ((0xFE == Convert.ToInt16(pbValue[0])) &&
                     (0xFF == Convert.ToInt16(pbValue[1])))
                {
                    Value = "UTF-16LE BOM+";

                    if (4 <= dwValueLen)
                    {
                        for (int i = 0; i < pbValue.Length - 2; i += 2)
                        {
                            Value += Convert.ToString(BitConverter.ToChar(pbValue, i));
                        }
                    }
                }
                else if ((0xFF == Convert.ToInt16(pbValue[0])) &&
                          (0xFE == Convert.ToInt16(pbValue[1])))
                {
                    Value = "UTF-16BE BOM+";
                    if (4 <= dwValueLen)
                    {
                        for (int i = 0; i < pbValue.Length - 2; i += 2)
                        {
                            Value += Convert.ToString(BitConverter.ToChar(pbValue, i));
                        }
                    }
                }
                else
                {
                    Value = "";
                    if (2 <= dwValueLen)
                    {
                        for (int i = 0; i < pbValue.Length - 2; i += 2)
                        {
                            Value += Convert.ToString(BitConverter.ToChar(pbValue, i));
                        }
                    }
                }
            }//end else not a 0-length string

            return Value;

        }//end method

    }//end class

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kris Rudin
Web Developer
United States United States
Kris Rudin is a senior developer with Ascentium corporation, with 10 years of experience including both web and Windows client software development.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 21 Jun 2006
Article Copyright 2006 by Kris Rudin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid