Click here to Skip to main content
6,629,377 members and growing! (21,911 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » DirectShow     Intermediate

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

By bjhamltn_BJH

Using DirectShow in a Webcam Capture Application
C++, Windows, .NET, Visual Studio, MFC, WinForms, Dev
Version:2 (See All)
Posted:13 May 2007
Updated:19 Jun 2007
Views:44,703
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.76 Rating: 3.27 out of 5
1 vote, 14.3%
1

2
1 vote, 14.3%
3
3 votes, 42.9%
4
2 votes, 28.6%
5
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


Member

Location: United States United States

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralNeed help with converting the captured photo into bytes. PinmemberAirkid21:55 11 Nov '09  
GeneralGrab image problem PinmemberMember 224900123:03 5 Oct '09  
GeneralExternal device not work Pinmemberislamicsoft4:30 30 Sep '09  
QuestionDoes not build PinmemberJackSimmons13:09 24 May '09  
AnswerRe: Does not build Pinmemberdave_mm06:38 4 Jun '09  
GeneralRe: Does not build PinmemberJackSimmons23:25 4 Jun '09  
QuestionResolution of webcam? PinmemberMember 466107923:01 19 Apr '09  
GeneralQuestion... using two cams PinmemberLoink13:29 6 Feb '09  
GeneralRe: Question... using two cams [modified] PinmemberMember 371271211:53 20 Feb '09  
GeneralHow to know if my webcam suuports Directshow Pinmembercherchor14:33 24 Nov '08  
QuestionAdd a second window with the preview Pinmembermister0zorg7:36 11 Nov '08  
GeneralGet Frame Streams by a callback PinmemberHaniSalehi20:48 8 Nov '08  
GeneralRe: Get Frame Streams by a callback Pinmembermister0zorg6:57 13 Nov '08  
Questionblank capture, assistance please! Pinmembersuefyzah20:50 20 Aug '08  
GeneralApplication Fail to start PinmemberHJC3:19 3 Jun '08  
GeneralRe: Application Fail to start Pinmembermister0zorg7:07 13 Nov '08  
QuestionCompilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberSaiyanKing17:26 18 Sep '07  
AnswerRe: Compilation error KSCATEGORY_AUDIO_DEVICE [modified] PinmemberPapuc Laszlo1:27 11 Nov '09  
GeneralNice work. PinmemberDr. Pastor14:20 29 Aug '07  
QuestionHelp me !!! Do you have the include files (*.h)? Pinmemberzyberas6:34 17 Aug '07  
Generalresolution PinmemberGyorgy Hevizi13:54 22 Jun '07  
GeneralThanks for the example! Compiles, runs, but crashes during grabbing Pinmemberrichard.hasty14:10 19 Jun '07  
GeneralRe: Thanks for the example! Compiles, runs, but crashes during grabbing Pinmemberbjhamltn_BJH17:08 19 Jun '07  
GeneralRe: Thanks for the example! Compiles, runs, but crashes during grabbing Pinmemberrichard.hasty18:09 19 Jun '07  
GeneralRe: Thanks for the example! Compiles, runs, but crashes during grabbing Pinmemberbeardedbernard0:12 22 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jun 2007
Editor: Deeksha Shenoy
Copyright 2007 by bjhamltn_BJH
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project