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.
Make sure Not using pre-complied headers is selected (optional).
Make sure the additional dependencies are added.
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;
hr = test.intCaptureGraphBuilder2();
hr = test.intGraphBuilder();
hr = test.intCreateDevEnum();
hr = test.QueryIMediaControl();
hr = test.QueryIMediaEvent();
hr = test.QueryIVideoWindow();
hr = test.QueryIMediaFilter();
hr = test.pCaptureGraph2->SetFiltergraph(test.pGraphBuilder);
hr = test.VideoCaptureSources();
vector<bstr> filternames = (test.getFilterName());
test.Filter_Bind(filternames[0]);
hr = test.intSampleGrabber();
hr = test.pGraphBuilder->AddFilter(test.pSG_Filter, L"SampleGrab");
hr = test.pCaptureGraph2->RenderStream
(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,
test.pBaseFilter,test.pSG_Filter,NULL);
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));
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();
test.wait(.5);
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;
this->Start_CamCap->Enabled = false;
this->Grab->Enabled = true;
this->stop_bnt->Enabled = true;
}
History
No updates planned at the moment
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.