Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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]
    test.Filter_Bind(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
		(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video, 
		test.pBaseFilter,test.pSG_Filter,NULL);
        
        
    // 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
    test.wait(.5);

    // 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;         
}

History

No updates planned at the moment

License

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

Share

About the Author

bjhamltn_BJH

United States United States
No Biography provided

Comments and Discussions

 
AnswerRe: Two webcam test [modified] Pinmembermathewmefiu13-Jan-10 11:49 
GeneralNeed help with converting the captured photo into bytes. PinmemberAirkid11-Nov-09 21:55 
GeneralGrab image problem PinmemberMember 22490015-Oct-09 23:03 
GeneralExternal device not work Pinmemberislamicsoft30-Sep-09 4:30 
QuestionDoes not build PinmemberJackSimmons24-May-09 13:09 
AnswerRe: Does not build Pinmemberdave_mm04-Jun-09 6:38 
GeneralRe: Does not build PinmemberJackSimmons4-Jun-09 23:25 
QuestionResolution of webcam? PinmemberMember 466107919-Apr-09 23:01 
AnswerRe: Resolution of webcam? Pinmembermerano25-Apr-10 10:53 
GeneralQuestion... using two cams PinmemberLoink6-Feb-09 13:29 
GeneralRe: Question... using two cams [modified] PinmemberMember 371271220-Feb-09 11:53 
QuestionHow to know if my webcam suuports Directshow Pinmembercherchor24-Nov-08 14:33 
QuestionAdd a second window with the preview Pinmembermister0zorg11-Nov-08 7:36 
GeneralGet Frame Streams by a callback PinmemberHaniSalehi8-Nov-08 20:48 
GeneralRe: Get Frame Streams by a callback Pinmembermister0zorg13-Nov-08 6:57 
Questionblank capture, assistance please! Pinmembersuefyzah20-Aug-08 20:50 
GeneralApplication Fail to start PinmemberHJC3-Jun-08 3:19 
GeneralRe: Application Fail to start Pinmembermister0zorg13-Nov-08 7:07 
QuestionCompilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberSaiyanKing18-Sep-07 17:26 
AnswerRe: Compilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberPapuc Laszlo11-Nov-09 1:27 
GeneralRe: Compilation error KSCATEGORY_AUDIO_DEVICE Pinmembermerano25-Apr-10 8:52 
GeneralRe: Compilation error KSCATEGORY_AUDIO_DEVICE PinmemberRevKeaponLaffin5-Jun-10 19:58 
GeneralNice work. PinmemberDr. Pastor29-Aug-07 14:20 
QuestionHelp me !!! Do you have the include files (*.h)? Pinmemberzyberas17-Aug-07 6:34 
Generalresolution PinmemberGyorgy Hevizi22-Jun-07 13:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141223.1 | Last Updated 19 Jun 2007
Article Copyright 2007 by bjhamltn_BJH
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid