2

I'm trying to read the image size of a WMV file in C#.
I've tried using what is described here:
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.

Community
  • 1
  • 1
Shachar Weis
  • 936
  • 2
  • 20
  • 44

3 Answers3

1

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.

user194743
  • 616
  • 5
  • 7
0

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
  • 52,691
  • 28
  • 123
  • 168
  • That does not compile: 'WMPLib.IWMPMedia.imageSourceHeight.get': cannot explicitly call operator or accessor – Shachar Weis Jun 19 '10 at 01:35
  • and mediaInfo.imageSourceHeight returns zero. – Shachar Weis Jun 19 '10 at 02:09
  • He's right: unless the file is **playing**, imageSourceHeight and imageSourceWidth return 0. – egrunin Jun 19 '10 at 04:10
  • 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 Jun 19 '10 at 12:04
0

I prefer to use the free NReco.VideoInfo.dll. Mainly because I hate Windows Media Player. I have found that WMP is unreliable.

Here is the download link: http://www.nrecosite.com/video_info_net.aspx It's useful for other stuff too.

var ffProbe = new NReco.VideoInfo.FFProbe();
var videoInfo = ffProbe.GetMediaInfo(pathToFile);
Int32 tmpHeight = videoInfo.Streams[0].Height;
Int32 tmpWidth = videoInfo.Streams[0].Width;
davehale23
  • 4,374
  • 2
  • 27
  • 40