Click here to Skip to main content
15,513,489 members
Articles / Desktop Programming / Windows Forms
Article
Posted 13 May 2007

Stats

177.7K views
9.8K downloads
54 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
19 Jun 20073 min read
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

Image 5

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.

C++
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMay I have this code in the Visual C++ 2010 Pin
osilator9-Dec-17 2:35
osilator9-Dec-17 2:35 
Question480i to 1080p ! Pin
Member 1102098119-Aug-14 2:10
Member 1102098119-Aug-14 2:10 
QuestionRecord Video Pin
Wolkenpaul23-Dec-13 5:52
Wolkenpaul23-Dec-13 5:52 
QuestionIs it possible to get the update code? Pin
Member 948883821-Nov-12 2:51
Member 948883821-Nov-12 2:51 
QuestionQedit.h Pin
MUHAMMAD FADZIL11-Jul-12 18:06
MUHAMMAD FADZIL11-Jul-12 18:06 
AnswerRe: Qedit.h Pin
christmars18-Sep-12 23:14
christmars18-Sep-12 23:14 
AnswerRe: Qedit.h Pin
christmars4-Oct-12 23:10
christmars4-Oct-12 23:10 
GeneralAdding hough transform Pin
Member 234773313-Jun-11 21:11
Member 234773313-Jun-11 21:11 
Generalcall stack Pin
adit_m9-Aug-10 9:19
adit_m9-Aug-10 9:19 
Generalchanging buffer content Pin
mateusz.matyaszek6-Feb-10 14:53
mateusz.matyaszek6-Feb-10 14:53 
QuestionTwo webcam test Pin
guybrush_8414-Dec-09 1:27
guybrush_8414-Dec-09 1:27 
AnswerRe: Two webcam test [modified] Pin
mateusz.matyaszek13-Jan-10 11:49
mateusz.matyaszek13-Jan-10 11:49 
GeneralNeed help with converting the captured photo into bytes. Pin
Airkid11-Nov-09 21:55
Airkid11-Nov-09 21:55 
GeneralGrab image problem Pin
Member 22490015-Oct-09 23:03
Member 22490015-Oct-09 23:03 
GeneralExternal device not work Pin
islamicsoft30-Sep-09 4:30
islamicsoft30-Sep-09 4:30 
QuestionDoes not build Pin
JackSimmons24-May-09 13:09
JackSimmons24-May-09 13:09 
AnswerRe: Does not build Pin
dave_mm04-Jun-09 6:38
dave_mm04-Jun-09 6:38 
GeneralRe: Does not build Pin
JackSimmons4-Jun-09 23:25
JackSimmons4-Jun-09 23:25 
QuestionResolution of webcam? Pin
Rick204719-Apr-09 23:01
Rick204719-Apr-09 23:01 
AnswerRe: Resolution of webcam? Pin
merano25-Apr-10 10:53
merano25-Apr-10 10:53 
GeneralQuestion... using two cams Pin
Loink6-Feb-09 13:29
Loink6-Feb-09 13:29 
GeneralRe: Question... using two cams [modified] Pin
eharbo20-Feb-09 11:53
eharbo20-Feb-09 11:53 
QuestionHow to know if my webcam suuports Directshow Pin
cherchor24-Nov-08 14:33
cherchor24-Nov-08 14:33 
QuestionAdd a second window with the preview Pin
mister0zorg11-Nov-08 7:36
mister0zorg11-Nov-08 7:36 
GeneralGet Frame Streams by a callback Pin
HaniSalehi8-Nov-08 20:48
HaniSalehi8-Nov-08 20:48 

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.