Click here to Skip to main content
11,934,389 members (71,582 online)
Click here to Skip to main content
Add your own
alternative version


442 bookmarked


, 22 Jul 2002 Public Domain
Rate this:
Please Sign up or sign in to vote.
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 
    // 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( 
                "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

You may also be interested in...

Comments and Discussions

Questionhow to code in c# to save webcam capture video to avi file using DShowNET Pin
roddam harini10-Sep-15 0:23
memberroddam harini10-Sep-15 0:23 
QuestionDispose doesn't work Pin
i0027-Jan-15 15:50
professional i0027-Jan-15 15:50 
QuestionHow to give notifications for the user if he captures the still image in full screen mode. Pin
sabarinew79027-May-13 4:03
membersabarinew79027-May-13 4:03 
QuestionPlay video using DIrectShow without using ActiveMovieWindow Pin
morandeira24-Feb-13 22:27
membermorandeira24-Feb-13 22:27 
GeneralMy vote of 5 Pin
Member 470135319-Feb-13 7:33
memberMember 470135319-Feb-13 7:33 
QuestionCapturing compressed stream Pin
Berlus13-Feb-13 11:04
memberBerlus13-Feb-13 11:04 
Questionpls. help Pin
martinmat5-Feb-13 4:08
membermartinmat5-Feb-13 4:08 
GeneralMy vote of 5 Pin
Pardazan8-Dec-12 20:53
memberPardazan8-Dec-12 20:53 
GeneralMy vote of 5 Pin
Mazen el Senih12-Nov-12 6:52
memberMazen el Senih12-Nov-12 6:52 
QuestionCommercial Use Pin
yrt5rtyhgjgh5-Oct-12 6:08
memberyrt5rtyhgjgh5-Oct-12 6:08 
GeneralMy vote of 5 Pin
khosrow parizi19-Sep-12 8:00
memberkhosrow parizi19-Sep-12 8:00 
QuestionAudio Dificulties Pin
FernandoCantu14-Sep-12 17:08
memberFernandoCantu14-Sep-12 17:08 
QuestionSampleGrabberNET Pin
Member 81139705-Sep-12 21:35
memberMember 81139705-Sep-12 21:35 
GeneralMy vote of 4 Pin
sabinne5-Jul-12 6:43
membersabinne5-Jul-12 6:43 
BugBroken Link Pin
amitgajjar6-Jun-12 21:02
memberamitgajjar6-Jun-12 21:02 
QuestionWant to change the Bit Rate or reduce file size. Pin
ssyuvaraja6-Jun-12 19:15
memberssyuvaraja6-Jun-12 19:15 
Questionmy vote of 5 Pin
muhamad yousef4-Jun-12 20:50
membermuhamad yousef4-Jun-12 20:50 
QuestionPins need to be fully disconnect to really close all streaming connections Pin
Ivan Olmos7-Mar-12 15:04
memberIvan Olmos7-Mar-12 15:04 
QuestionCan't navigate to the 1st frame Pin
Squall836-Mar-12 4:32
memberSquall836-Mar-12 4:32 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 19:21
membermanoj kumar choubey26-Feb-12 19:21 
GeneralMy vote of 5 Pin
Member 819981417-Sep-11 6:28
memberMember 819981417-Sep-11 6:28 
Questiongreat work Pin
Member 468250616-Aug-11 2:00
memberMember 468250616-Aug-11 2:00 
Questionselect output device Pin
kinani2-Aug-11 6:48
memberkinani2-Aug-11 6:48 
Generaldirectshowlib Pin
hulyaaa15-Jun-11 3:54
memberhulyaaa15-Jun-11 3:54 
GeneralHow to create playlist for this media player... Pin
kaushik24027-Jun-11 4:24
memberkaushik24027-Jun-11 4:24 
QuestionHow to create audio spectrum with Pin
davidkz235-Jun-11 6:26
memberdavidkz235-Jun-11 6:26 
Questionhow to disconnect from the webcame using directshow Pin
tofa1115-Apr-11 8:40
membertofa1115-Apr-11 8:40 
AnswerRe: how to disconnect from the webcame using directshow Pin
Nem9-May-11 0:54
memberNem9-May-11 0:54 
AnswerRe: how to disconnect from the webcame using directshow Pin
i0027-Jan-15 15:48
professional i0027-Jan-15 15:48 
QuestionHow can I use DirectShow.Net in ? Pin
rakesh_sharnagate1-Apr-11 23:10
memberrakesh_sharnagate1-Apr-11 23:10 
AnswerRe: How can I use DirectShow.Net in ? Pin
IssamK30-May-11 23:23
memberIssamK30-May-11 23:23 
GeneralCapture image and Record file Pin
Mdgazanfar7-Mar-11 22:06
memberMdgazanfar7-Mar-11 22:06 
GeneralI get this error Pin
leonkennedy24-Feb-11 9:56
memberleonkennedy24-Feb-11 9:56 
GeneralPlay Live video Stream Pin
Member 341395222-Feb-11 2:50
memberMember 341395222-Feb-11 2:50 

Can u please tell me how to play stream video.
Thanks in advance
Chintan S. Pandya

QuestionOverlay image on video Pin
jymitra14-Feb-11 9:06
memberjymitra14-Feb-11 9:06 
QuestionHow change resolution video after? [modified] Pin
Demaker8-Feb-11 3:40
memberDemaker8-Feb-11 3:40 
GeneralSound is Missing... Pin
AliAmjad7-Feb-11 3:56
memberAliAmjad7-Feb-11 3:56 
GeneralGrabb image from .mpg video file using directshow Pin
Mdgazanfar7-Jan-11 2:31
memberMdgazanfar7-Jan-11 2:31 
GeneralRe: Grabb image from .mpg video file using directshow Pin
lirvine6-Jul-11 7:33
memberlirvine6-Jul-11 7:33 
GeneralRecording also from Output Device (SoundCard) Pin
pHr0ZZy21-Dec-10 7:08
memberpHr0ZZy21-Dec-10 7:08 
GeneralMy vote of 5 Pin
sunzhuo19813-Nov-10 21:50
membersunzhuo19813-Nov-10 21:50 
GeneralResolution 704 X 576 supports in Avermedia PCI CARD [modified] Pin
vicky20081-Nov-10 21:48
membervicky20081-Nov-10 21:48 
QuestionTake a snapshot from a large file (lets say 5 gigs) Pin
chen_kinnrot2-Sep-10 20:15
memberchen_kinnrot2-Sep-10 20:15 
GeneralLoading a GRF file [modified] Pin
maggik31-Aug-10 23:48
membermaggik31-Aug-10 23:48 
GeneralRe: Loading a GRF file Pin
maggik1-Sep-10 2:50
membermaggik1-Sep-10 2:50 
GeneralFYI, using a different audio device / different sound card for playback Pin
rnendel12-Aug-10 16:30
memberrnendel12-Aug-10 16:30 
GeneralRe: FYI, using a different audio device / different sound card for playback Pin
Member 324083028-Jul-11 0:39
memberMember 324083028-Jul-11 0:39 
GeneralWindows 7 64bit Pin
divyesh14322-Aug-10 21:09
memberdivyesh14322-Aug-10 21:09 
GeneralCapture DVD frames. [modified] Pin
Member 472252317-Jul-10 23:07
memberMember 472252317-Jul-10 23:07 
GeneralShow the selected button in the DVD-Menu. Pin
Hugo_Gray16-Feb-10 13:05
memberHugo_Gray16-Feb-10 13:05 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 23 Jul 2002
Article Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid