8.11 Show an Animation with DirectShow


Problem

You need to play a video file (such as an MPEG, an AVI, or a WMV file) in a Windows form.

Solution

Use the ActiveMovie COM component included with Media Player. Bind the video output to a picture box on your form by setting the IVideoWindow.Owner property to the PictureBox.Handle property.

Discussion

Although the .NET Framework does not include any managed classes for interacting with video files, you can leverage the functionality of DirectShow using the COM-based Quartz library included with Windows Media Player and the Windows operating system. For information about creating an interop assembly for the Quartz type library, refer to the instructions in recipe 8.10.

Once you have created the interop assembly, you can use the IMediaControl interface to load and play a movie. This is essentially the same technique demonstrated in recipe 8.10 with audio files. However, if you want to show the video window inside your application interface (rather than in a separate stand- alone window), you must also use the IVideoWindow interface. The core FilgraphManager object can be cast to both the IMediaControl and the IVideoWindow interface ”and several other interfaces are also supported, such as IBasicAudio (which allows you to configure balance and volume settings). With the IVideoWindow interface, you can bind the video output to an object on your form, such as a panel or a picture box. To do so, set the IVideoWindow.Owner property to the handle for the control, which you can retrieve using the Control.Handle property. Then, call IVideoWindow.SetWindowPosition to set the window size and location. This method can be called to change the video size changes during playback (for example, if the form is resized).

The following example shows a simple form that allows users to open any video file and play it back in the provided picture box. The picture box is anchored to all sides of the form, so it changes size as the form is resized. The code responds to the PictureBox.SizeChanged event to change the size of the corresponding video window.

 using System; using QuartzTypeLib; using System.Windows.Forms; public class ShowMovie : System.Windows.Forms.Form {     private System.Windows.Forms.PictureBox pictureBox1;     private System.Windows.Forms.Button cmdOpen;     // (Designer code omitted.)     // Define constants used for specifying the window style.     private const int WM_APP = 0x8000;     private const int WM_GRAPHNOTIFY = WM_APP + 1;     private const int EC_COMPLETE = 0x01;     private const int WS_CHILD = 0x40000000;     private const int WS_CLIPCHILDREN = 0x2000000;     // Hold a form-level reference to the media control interface,     // so the code can control playback of the currently loaded     // movie.     private IMediaControl mc = null;     // Hold a form-level reference to the video window in case it     // needs to be resized.     private IVideoWindow videoWindow = null;     private void cmdOpen_Click(object sender, System.EventArgs e) {              // Allow the user to choose a file.         OpenFileDialog openFileDialog = new OpenFileDialog();         openFileDialog.Filter =          "Media Files*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3All Files*.*";         if (DialogResult.OK == openFileDialog.ShowDialog()) {                      // Stop the playback for the current movie, if it exists.             if (mc != null) mc.Stop();             // Load the movie file.             FilgraphManager graphManager = new FilgraphManager();             graphManager.RenderFile(openFileDialog.FileName);             // Attach the view to a picture box on the form.             try {                              videoWindow = (IVideoWindow)graphManager;                 videoWindow.Owner = (int) pictureBox1.Handle;                 videoWindow.WindowStyle = WS_CHILD  WS_CLIPCHILDREN;                 videoWindow.SetWindowPosition(                   pictureBox1.ClientRectangle.Left,                   pictureBox1.ClientRectangle.Top,                   pictureBox1.ClientRectangle.Width,                   pictureBox1.ClientRectangle.Height);             }catch {                              // An error can occur if the file does not have a video                 // source (for example, an MP3 file.)                 // You can ignore this error and still allow playback to                 // continue (without any visualization).             }             // Start the playback (asynchronously).             mc = (IMediaControl)graphManager;             mc.Run();         }     }     private void pictureBox1_SizeChanged(object sender, System.EventArgs e) {              if (videoWindow != null) {                      try {                 videoWindow.SetWindowPosition(                     pictureBox1.ClientRectangle.Left,                     pictureBox1.ClientRectangle.Top,                     pictureBox1.ClientRectangle.Width,                     pictureBox1.ClientRectangle.Height);             }catch {                 // Ignore the exception thrown when resizing the form                 // when the file does not have a video source.             }         }     }     private void ShowMovie_Closing(object sender,       System.ComponentModel.CancelEventArgs e) {              if (mc != null) mc.Stop();     } } 

An example of the output you'll see is shown in Figure 8.7.


Figure 8.7: Playing a video file.



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net