tags:

views:

101

answers:

2

Hello,
I'm trying to read the image size of a WMV file in C#.
I've tried using what is described here:
http://stackoverflow.com/questions/2445939/how-do-i-get-the-duration-of-a-video-file-using-c
but the only attribute that has a value is Duration.

Any ideas ?
Thanks.

A: 

You use the code from the linked example, but you explicitly do a function call to get the height and width.

Example:

using WMPLib; // this file is called Interop.WMPLib.dll

WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv"); 

long height, width;
mediaInfo.get_imageSourceHeight(height);
mediaInfo.get_imageSourceWidth(width);
Byron Whitlock
That does not compile:'WMPLib.IWMPMedia.imageSourceHeight.get': cannot explicitly call operator or accessor
Shachar Weis
and mediaInfo.imageSourceHeight returns zero.
Shachar Weis
He's right: unless the file is **playing**, imageSourceHeight and imageSourceWidth return 0.
egrunin
It seems like such a simple thing, yet after a few hours of digging and trying I am no where closer to solving this.There is almost no documentation about these classes, its very sad.Playing the file is not simple, the application does not have a Form.Anyone has any ideas ?
Shachar Weis
+1  A: 

Only way I've seen it done is by playing it and attaching to the open event:

    static WindowsMediaPlayerClass player;

    static void Main()
    {
        player = new WindowsMediaPlayerClass();  
        IWMPMedia mediaInfo = player.newMedia("test.wmv");
        player.OpenStateChange += new _WMPOCXEvents_OpenStateChangeEventHandler(player_OpenStateChange);
        player.currentMedia = mediaInfo;

        //...

        Console.WriteLine("Done.");
        Console.ReadKey();
    }

    private static void player_OpenStateChange(int state)
    {
        if (state == (int)WMPOpenState.wmposMediaOpen)
        {
            Console.WriteLine( "height = " + player.currentMedia.imageSourceHeight);
            Console.WriteLine( "width = " + player.currentMedia.imageSourceWidth);
        }
    }

You'll want to dispose of any resources before exiting.