Subscribe

RSS Feed (xml)

Showing Animation with DirectShow

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

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 here.

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 this post 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;*.mp3|All 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 the below figure.

videofile.JPG

Technorati :

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail