Click here to Skip to main content
14,489,976 members


Rate this:
4.94 (191 votes)
Please Sign up or sign in to vote.
4.94 (191 votes)
22 Jul 2002Public Domain
DirectShow for DVD and file playback, capture and sample grabber

Sample Image - directshownet.jpg

Disclaimer: Experimental code using DirectShow with the .NET Framework 1.0


This experimental code shows how to use DirectShow with .NET and C#. This includes simple media playback, playing DVD discs, capturing video streams to disk and a sample picture grabber.

Note, this article doesn't save you from reading the detailed DirectShow SDK documentation! I will not explain DirectShow, only some of the used .NET Interop technologies!


DirectShow is a standardized Microsoft Win32 API to use any compliant movie or video device from your application. DirectShow is available with the current DirectX version 8.1(b) for Windows 98/ME/2000 and included in XP. Please install the latest version, this article doesn't support anything except 8.1 :

Again, I will not describe any DirectShow interfaces, you have to know them by installing the SDK for C++, reading the SDK documentation and understanding the SDK samples!

DirectShow is exposed as COM components and interfaces, at these two 'levels':

  • DirectShow custom interfaces - mainly for C++ programmers.
  • DirectShow VB components - designed for VB6, provides a type library.
You can use the DirectShow playback components for VB6 with .NET, as described in this CodeProject article: DirectShow MediaPlayer in C# (Daniel Strigl)

.NET Interop

While using the VB6 components with the provided type library is easy with .NET, there is no direct way to access the custom DirectShow interfaces. We have to use Interop with one of this approaches:

  • Use 'Managed Extensions for C++', as done e.g. by DirectX.NET
  • Rewrite all the interfaces from IDL to e.g. C# !
I chose the second strategy for this reasons :
  • Uses only one (managed) language (C#)
  • Most DirectShow interfaces are not very complex
  • DirectShow methods for simple playback/capturing are not time-critical
  • We can directly use the (documented) interfaces without limitations, no 'wrapper classes'
Sure, this has some drawbacks:
  • Much of initial work for rewriting the interfaces
  • You have to understand Interop to use it correctly
  • Not very .NET/OO-like
One typical rewrite of an IDL interface in C# looks like this :
// ======== IDL of ICaptureGraphBuilder2 (AXExtend.idl) ======
interface ICaptureGraphBuilder2 : IUnknown {

    // Use this filtergraph
    HRESULT SetFiltergraph( [in] IGraphBuilder *pfg );

    // what filtergraph are you using?
    // *ppfg->Release() when you're done with it
    HRESULT GetFiltergraph( [out] IGraphBuilder **ppfg);
... using Interop attributes with C# will be translated to :
// ======== C# version of ICaptureGraphBuilder2 (DsExtend.cs) ======

   [ComVisible(true), ComImport,
    InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface ICaptureGraphBuilder2
   int SetFiltergraph( [In] IGraphBuilder pfg );

   int GetFiltergraph( [Out] out IGraphBuilder ppfg );

Once we have all this interface definitions in C#, we can start calling DirectShow just like we did in C++:

// ======== C++ code to create the COM instance of Filter Graph ========

    JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                         IID_IGraphBuilder, (void **)&pGB));

    // Have the graph builder construct its the <BR>    // appropriate graph automatically
    JIF(pGB->RenderFile(wFile, NULL));

    // QueryInterface for DirectShow interfaces
    JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
... we replace CoCreateInstance with Activator.CreateInstance, and QueryInterface just is a simple cast in C#:
// ======== C# code to create the COM instance of Filter Graph ========

    Type comtype = null;
    object comobj = null;
    try {
        comtype = Type.GetTypeFromCLSID( Clsid.FilterGraph );
        if( comtype == null )
            throw new NotSupportedException( <BR>                "DirectX (8.1 or higher) not installed?" );
        comobj = Activator.CreateInstance( comtype );
        graphBuilder = (IGraphBuilder) comobj; comobj = null;
        int hr = graphBuilder.RenderFile( clipFile, null );
        if( hr < 0 )
            Marshal.ThrowExceptionForHR( hr );

        mediaCtrl    = (IMediaControl)  graphBuilder;

Projects Structure

The download contains all this C# source code:

     \DShowNET\              // the DirectShow interface definitions :
              \DsBugWO.cs      // workaround for a bug 
              \DsControl.cs    // ported from control.odl 
              \DsCore.cs       // ported from axcore.idl 
              \DsDevice.cs     // device enumerator, helper functions 
              \DsDVD.cs        // DVD interfaces from dvdif.idl 
              \DsExtend.cs     // ported from axextend.idl 
              \DsUtils.cs      // utility classes, SDK Common sources 
              \DsUuids.cs      // UUIDs and CLSIDs from uuids.h 
              \QEdit.cs        // grabber interfaces from qedit.idl 

     \CaptureNET\            // video stream capture sample 
     \DVDPlayerNET\          // DVD player sample 
     \PlayWndNET\            // simple media file playback 
     \SampleGrabberNET\      // picture grabber 


The first sample included in the download is PlayWndNET. It plays the known video and audio file formats of DirectShow like avi, mpg, wav, mid etc.

DirectShow playback

DVD Player

For the next sample, DVDPlayerNET you must have a third-party DVD codec installed, like WinDVD or PowerDVD. Then, the C# sample uses the DirectShow DVD interfaces to watch the movie. It also supports menu navigation.

DirectShow DVD

Grab Picture

The most complex sample provided is SampleGrabberNET. It shows a live video stream from a capture device like DV cam, web cam or TV card in a preview window. By pressing the 'Grab' toolbar-button, you can capture a still picture to a 24-Bit RGB bitmap file!

DirectShow picture grabber

The sample also supports the IAMTVTuner interface of a TV card, so you can switch the TV tuner channel.


The last sample, CaptureNET can be used to capture a live video stream to disk. Note, the few settings can only be done once at startup, and writing to the AVI file starts immediately.

DirectShow Capturing


  • EXPERIMENTAL! don't use it in production quality code.
  • The samples only provide partial and very basic functionality.
  • I did most tests on Windows XP and few on Windows ME.
  • Tested only on a very limited set of devices with only few media formats.
    I used a Logitech QuickCam, Sony DV camcorder, Hauppauge WinTV PCI and WinDVD.
  • Get the latest driver (WDM) from manufacturer.
  • Some devices fail if you select unsupported settings in the dialogs.
  • This code will NOT help to solve any DirectShow/WDM configuration problems.
  • Get >128MB RAM, >400MHz CPU, fast & huge harddisk.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Web Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

GeneralSampleGrabberNET help needed Pin
Anonymous13-May-04 9:40
MemberAnonymous13-May-04 9:40 
GeneralActiveMovie Application Error Pin
Cunk6-May-04 7:14
MemberCunk6-May-04 7:14 
GeneralRe: ActiveMovie Application Error Pin
LeTchi27-May-04 11:26
MemberLeTchi27-May-04 11:26 
GeneralRe: ActiveMovie Application Error Pin
Cunk8-Jun-04 0:11
MemberCunk8-Jun-04 0:11 
GeneralProblem with creating the filter graph more than once in my program Pin
Member 9709685-May-04 11:33
MemberMember 9709685-May-04 11:33 
GeneralRe: Problem with creating the filter graph more than once in my program Pin
ravinamballa24-Jun-04 15:03
Memberravinamballa24-Jun-04 15:03 
GeneralRe: Problem with creating the filter graph more than once in my program Pin
2-Jul-04 4:33
suss2-Jul-04 4:33 
GeneralRe: Problem with creating the filter graph more than once in my program Pin
Anonymous29-Jun-04 5:53
MemberAnonymous29-Jun-04 5:53 
I finally got through this problem today.

Initially, I was releasing all the objects and recreating the graph again for the device each time I needed to switch between "Grabbing Image" and "Saving video".

But, now, instead of releasing and recreating the whole graph, I have taken a new approach, which is

- Stop the mediacontrol.

- Use GraphBuilder.RemoveFilter() to remove the CapFilter and any other filter added to the graphbuilder.

- Add the CapFilter and other filter (like muxfilter or grabfilter) needed and then run the renderstream.

- Setup the videowindow again.

- run mediacontrol.

This works well in the same form for me.

Hope this is helpful.


GeneralHelp me Pin
Anonymous4-May-04 16:15
MemberAnonymous4-May-04 16:15 
GeneralRe: Help me Pin
eggie54-May-04 16:48
Membereggie54-May-04 16:48 
GeneralViewing content directly from database Pin
Chan Achanthodi4-May-04 10:15
MemberChan Achanthodi4-May-04 10:15 
GeneralSlow Motion Issue Help Please Pin
jason savage30-Apr-04 23:09
Memberjason savage30-Apr-04 23:09 
GeneralRe: Slow Motion Issue Help Please Pin
sgoodlass2-May-04 11:33
Membersgoodlass2-May-04 11:33 
Generalhelp using sampGrabber.GetCurrentBuffer Pin
sgoodlass29-Apr-04 17:15
Membersgoodlass29-Apr-04 17:15 
GeneralRe: help using sampGrabber.GetCurrentBuffer Pin
LeTchi27-May-04 11:22
MemberLeTchi27-May-04 11:22 
GeneralRe: help using sampGrabber.GetCurrentBuffer Pin
LeTchi27-May-04 11:23
MemberLeTchi27-May-04 11:23 
GeneralConfigure Brightness without property pages Pin
Anonymous25-Apr-04 15:52
MemberAnonymous25-Apr-04 15:52 
GeneralRe: Configure Brightness without property pages Pin
Count_Nergal26-Oct-04 2:02
MemberCount_Nergal26-Oct-04 2:02 
QuestionAudio?? Pin
ajcg24-Apr-04 0:30
Memberajcg24-Apr-04 0:30 
QuestionHow to communicate between the MainApplaication and the Transformfilter? Pin
Count_Nergal23-Apr-04 2:21
MemberCount_Nergal23-Apr-04 2:21 
AnswerRe: How to communicate between the MainApplaication and the Transformfilter? Pin
furkinfedup10-Oct-04 23:19
Memberfurkinfedup10-Oct-04 23:19 
GeneralExtract Images from PlayerWnd Pin
Arun N Kumar9-Apr-04 0:05
MemberArun N Kumar9-Apr-04 0:05 
GeneralTrackbar Pin
SilvesterSmall4-Apr-04 21:39
MemberSilvesterSmall4-Apr-04 21:39 
Questionwhy are the first 2 param of ICaptureGraphBuilder2::RenderStream() be set to type &quot;ref&quot; in DShowNet? Pin
dotgi2-Apr-04 21:19
Memberdotgi2-Apr-04 21:19 
QuestionHow to get reference to a VideoComrepssor filter and set its properties? Pin
Mr.Naveed25-Mar-04 11:33
MemberMr.Naveed25-Mar-04 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 21 Jul 2002


450 bookmarked