Click here to Skip to main content
12,896,953 members (52,990 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 13 May 2007

Visual Studio C++ .NET Express: Using DirectShow in a Webcam Capture Application

, 19 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Using DirectShow in a Webcam Capture Application
Screenshot - Article.gif


This project is intended to make it easier for beginners to learn how to use DirectShow with Visual Studio C++ .NET Express. This project consist of a Windows Forms application that can be used to capture images from a webcam. From this project, along with the many other projects available on this site, you should be able to gain a better understanding of how to create your own DirectShow applications.

Starting a New Project for Use with DirectShow

Step 1: Start Visual Studio C++ .NET Express.

Step 2: Create new project.

Step 3: Select Windows Form as the project type.

As an alternative, you could just download the project and use it as your project template.

Configuring Properties Page

Make the following changes in the Property Pages:

Make sure Common Language Runtime Support is selected.

Screenshot - pp2.gif

Make sure Not using pre-complied headers is selected (optional).

Screenshot - pp1.gif

Make sure the additional dependencies are added.

Screenshot - pp3.gif

Also you may need to set your include directories as shown below. The order in which the directories are listed matters. Make sure The Microsoft Platform SDK is first. The path to this window is:

Tools->Options->Projects and Solutions->VC++ Directories

Using the Code

This code uses two header files, easyshowH.h and BHImaging.h. The header file easyshowH.h contains an unmanaged class of DirectShow functions and interfaces, the class is called BensShow but you can rename it to whatever you like. The BHImaging.h contains a managed class that I wrote to handle the bitmap data. I wrote my easyshowH.h class so that I wouldn't have to remember all of the details needed to use DirectShow. To use the class in the easyshowH.h file, you still need to have some knowledge of how DirectShow works, particularly how initialization, querying, and enumeration work. While using a class like the one in easyshowH.h means that you don't have to remember all of the technical things associated with DirectShow, like CLID identifiers and GUID types, it is still a good idea to have a look at them to find out what kind of things they are. This project should give you a basic understanding of how to create Windows Forms applications that can use DirectShow. Check the Platform SDK documentation to learn more about using DirectShow, as most of what I have written come from examples give in the Platform SDK documentation.

Usage Sample

This is only a part of the code from the project. Download the source or the binary from the links provided above.

When using, pay attention to the comments provided and the order of events.

In this project, very little error checking has been provided, If you intend to make extensive use of any of the code in this project, I would recommend that you put in place some form of error checking. In the case of this project, you could check the value of the HRESULT 'hr' value that is returned to see if the operation was successful.

private: System::Void Start_CamCap_Click(System::Object^  sender, System::EventArgs^  e) 
    BSTR  filtername;
    ULONG cFetched;    VARIANT varName;        
    //initializes the a CaptureGraphBuilder2

    hr = test.intCaptureGraphBuilder2();
    //initializes the a GraphBuilder

    hr = test.intGraphBuilder();
    //initializes the a SysDevEnum

    hr = test.intCreateDevEnum();
    //Query GraphBuilder for necessary Interfaces

    hr = test.QueryIMediaControl();
    hr = test.QueryIMediaEvent();
    hr = test.QueryIVideoWindow();
    hr = test.QueryIMediaFilter();

    //Specifies the CaptureGraphBuilder2 to use test.pGraphBuilder as its filter graph

    hr = test.pCaptureGraph2->SetFiltergraph(test.pGraphBuilder);
    //called in order to Enumerate all the Video capture devices installed on system

    hr = test.VideoCaptureSources();

    //Saves the names of the installed devices into a vector

    //the vector can be used to populate a list box

    //on my system I have only one video capture device

    vector<bstr> filternames = (test.getFilterName());

    //binds filter to device matching the name 

    // in this case a basefilter is bound to the device with name == filternames[0]


    //Add the Sample Grabber filter to the graph.

        //initializes the a sample grabber

        hr = test.intSampleGrabber();  
        //adds the sample grabber to the GraphBuilder

        hr = test.pGraphBuilder->AddFilter(test.pSG_Filter, L"SampleGrab"); 
        //connects the filers that are on the CaptureGraphBuilder2

        hr = test.pCaptureGraph2->RenderStream
    // Configure the Sample Grabber.

    hr = test.QueryISampleGrabber();
    hr = test.pSampleGrabber->SetOneShot(false);
    hr = test.pSampleGrabber->SetBufferSamples(true);
    hr = test.pSampleGrabber->SetMediaType(&(test.MediaType));
    hr = test.pSampleGrabber->GetConnectedMediaType(&(test.MediaType));
    //locks active window to form panel

    Rectangle rc = this->Play_Panel->ClientRectangle;
    hr = test.pWindow->put_Owner(OAHWND(this->Play_Panel->Handle.ToInt64()));
    hr = test.pWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN );
    hr = test.pWindow->SetWindowPosition( 0, 0, rc.Right, rc.Bottom );
    hr = hr = test.pMediaControl->Run();
    //Pause to let the window load


    // Sets initial conditions needed to grab frames

    // These setting are only when video is started

    // when a frame is grabbed do not need to be updated

    hr = test.cbBuffer = 0;
    hr = test.pSampleGrabber->GetCurrentBuffer(&(test.cbBuffer), NULL);
         test.pBuffer = new BYTE[test.cbBuffer];
         test.pVideoHeader = (VIDEOINFOHEADER*)test.MediaType.pbFormat;
         test.Bitmp_Info_header  = test.pVideoHeader->bmiHeader;
     // You cannot grab frames before you perform all of the steps above

     // You cannot stop video that is not playing

     // Make sure to properly order operations

         this->Start_CamCap->Enabled = false;
         this->Grab->Enabled = true;
         this->stop_bnt->Enabled = true;         


No updates planned at the moment


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Question480i to 1080p ! Pin
Member 1102098119-Aug-14 1:10
memberMember 1102098119-Aug-14 1:10 
QuestionRecord Video Pin
Wolkenpaul23-Dec-13 4:52
memberWolkenpaul23-Dec-13 4:52 
QuestionIs it possible to get the update code? Pin
Member 948883821-Nov-12 1:51
memberMember 948883821-Nov-12 1:51 
QuestionQedit.h Pin
MUHAMMAD FADZIL11-Jul-12 17:06
memberMUHAMMAD FADZIL11-Jul-12 17:06 
AnswerRe: Qedit.h Pin
christmars18-Sep-12 22:14
memberchristmars18-Sep-12 22:14 
AnswerRe: Qedit.h Pin
christmars4-Oct-12 22:10
memberchristmars4-Oct-12 22:10 
GeneralAdding hough transform Pin
Member 234773313-Jun-11 20:11
memberMember 234773313-Jun-11 20:11 
Generalcall stack Pin
adit_m9-Aug-10 8:19
memberadit_m9-Aug-10 8:19 
Generalchanging buffer content Pin
mathewmefiu6-Feb-10 13:53
membermathewmefiu6-Feb-10 13:53 
QuestionTwo webcam test Pin
guybrush_8414-Dec-09 0:27
memberguybrush_8414-Dec-09 0:27 
AnswerRe: Two webcam test [modified] Pin
mathewmefiu13-Jan-10 10:49
membermathewmefiu13-Jan-10 10:49 
GeneralNeed help with converting the captured photo into bytes. Pin
Airkid11-Nov-09 20:55
memberAirkid11-Nov-09 20:55 
GeneralGrab image problem Pin
Member 22490015-Oct-09 22:03
memberMember 22490015-Oct-09 22:03 
GeneralExternal device not work Pin
islamicsoft30-Sep-09 3:30
memberislamicsoft30-Sep-09 3:30 
QuestionDoes not build Pin
JackSimmons24-May-09 12:09
memberJackSimmons24-May-09 12:09 
AnswerRe: Does not build Pin
dave_mm04-Jun-09 5:38
memberdave_mm04-Jun-09 5:38 
GeneralRe: Does not build Pin
JackSimmons4-Jun-09 22:25
memberJackSimmons4-Jun-09 22:25 
QuestionResolution of webcam? Pin
Member 466107919-Apr-09 22:01
memberMember 466107919-Apr-09 22:01 
AnswerRe: Resolution of webcam? Pin
merano25-Apr-10 9:53
membermerano25-Apr-10 9:53 
GeneralQuestion... using two cams Pin
Loink6-Feb-09 12:29
memberLoink6-Feb-09 12:29 
GeneralRe: Question... using two cams [modified] Pin
Member 371271220-Feb-09 10:53
memberMember 371271220-Feb-09 10:53 
QuestionHow to know if my webcam suuports Directshow Pin
cherchor24-Nov-08 13:33
membercherchor24-Nov-08 13:33 
QuestionAdd a second window with the preview Pin
mister0zorg11-Nov-08 6:36
membermister0zorg11-Nov-08 6:36 
GeneralGet Frame Streams by a callback Pin
HaniSalehi8-Nov-08 19:48
memberHaniSalehi8-Nov-08 19:48 
GeneralRe: Get Frame Streams by a callback Pin
mister0zorg13-Nov-08 5:57
membermister0zorg13-Nov-08 5:57 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 19 Jun 2007
Article Copyright 2007 by bjhamltn_BJH
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid