Click here to Skip to main content
Licence 
First Posted 13 May 2007
Views 90,966
Downloads 3,782
Bookmarked 46 times

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

By | 19 Jun 2007 | Article
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

About the Author

bjhamltn_BJH



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAdding hough transform PinmemberMember 234773320:11 13 Jun '11  
Generalcall stack Pinmemberadit_m8:19 9 Aug '10  
Generalchanging buffer content Pinmembermathewmefiu13:53 6 Feb '10  
QuestionTwo webcam test Pinmemberguybrush_840:27 14 Dec '09  
AnswerRe: Two webcam test [modified] Pinmembermathewmefiu10:49 13 Jan '10  
GeneralNeed help with converting the captured photo into bytes. PinmemberAirkid20:55 11 Nov '09  
GeneralGrab image problem PinmemberMember 224900122:03 5 Oct '09  
GeneralExternal device not work Pinmemberislamicsoft3:30 30 Sep '09  
QuestionDoes not build PinmemberJackSimmons12:09 24 May '09  
AnswerRe: Does not build Pinmemberdave_mm05:38 4 Jun '09  
GeneralRe: Does not build PinmemberJackSimmons22:25 4 Jun '09  
QuestionResolution of webcam? PinmemberMember 466107922:01 19 Apr '09  
AnswerRe: Resolution of webcam? Pinmembermerano9:53 25 Apr '10  
GeneralQuestion... using two cams PinmemberLoink12:29 6 Feb '09  
GeneralRe: Question... using two cams [modified] PinmemberMember 371271210:53 20 Feb '09  
QuestionHow to know if my webcam suuports Directshow Pinmembercherchor13:33 24 Nov '08  
QuestionAdd a second window with the preview Pinmembermister0zorg6:36 11 Nov '08  
GeneralGet Frame Streams by a callback PinmemberHaniSalehi19:48 8 Nov '08  
GeneralRe: Get Frame Streams by a callback Pinmembermister0zorg5:57 13 Nov '08  
Questionblank capture, assistance please! Pinmembersuefyzah19:50 20 Aug '08  
GeneralApplication Fail to start PinmemberHJC2:19 3 Jun '08  
Great work. Works pretty well in the correct environment.
 
I am trying to run the exe on a machine that does not have VS or SDK's installed and then get this message:
 
"This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem."
 
Does anybody have an idea what files or runtime are required to run it properly? I tried installing WME9 and .Net CF 3.5 but to no avail.
 
Also I am trying to access three Logitech Webcams simultaneously but only one opens at a time. Is this a problem with the drivers?
Is there a way around it?
 
Thanks, HJC
GeneralRe: Application Fail to start Pinmembermister0zorg6:07 13 Nov '08  
QuestionCompilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberSaiyanKing16:26 18 Sep '07  
AnswerRe: Compilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberPapuc Laszlo0:27 11 Nov '09  
GeneralRe: Compilation error KSCATEGORY_AUDIO_DEVICE Pinmembermerano7:52 25 Apr '10  

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.

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