Share via


Using a Screen Capture as a Source

Windows Media Encoder SDK banner art

You can use a screen capture as a video source. The screen capture plug-in enables capture of all or part of the screen, for example, to capture mouse pointer movements to demonstrate a procedure. You can also capture from a card that is connected to a scan converter device.

You can configure the plug-in to specify the area you want to capture. One way is to use the IPropertyBag interface (see the C++ example below). This plug-in also supports a property page that enables configuration of the capture process by end users. To learn how to create a dialog box and display a property page, see Displaying the Plug-In Property Pages.

Consider the following guidelines to improve screen captures:

  • Use the Windows Media Video 9 Screen codec.
  • Use a fast graphics card.
  • Use a fast computer or a computer with a dual processor.
  • Disable hardware acceleration. From the Control Panel Display Properties, on the Settings tab, click Advanced. On the Troubleshoot tab, slide the Hardware acceleration setting to None.
  • Decrease color quality to reduce the amount of data that the graphics card must process. From the Control Panel Display Properties, on the Settings tab, click a lower setting for Color quality.
  • Run your encoding application in 256 colors. Right-click the executable file, click Properties, then on the Compatibility tab, select Run in 256 colors.
  • Modify the profile to reduce the frame rate and the image capture size. Increasing the bit rate can also result in better performance.
  • Close all unnecessary applications.
  • Disable the background picture.

The following Visual Basic example shows how to use a screen capture as a source. The C++ example shows how to configure the screen capture using the property bag. For end-to-end code examples, see Complete Code Examples.

Note When specifying the resource name, use the string "ScreenCap://ScreenCapture1" in the SetInput method of the IWMEncSource object.

Visual Basic Example

' Declare objects and variables.
  Dim Encoder as WMEncoder
  Dim SrcGrpColl As IWMEncSourceGroupCollection
  Dim SrcGrp As IWMEncSourceGroup
  Dim SrcVid As IWMEncVideoSource

' Create a WMEncoder object.
  Set Encoder = New WMEncoder

' Retrieve a source group collection.
  Set SrcGrpColl = Encoder.SourceGroupCollection

' Create a source group called SG_1.
  Set SrcGrp = SrcGrpColl.Add("SG_1")

' Create a video source object and an audio source object.
  Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO)

' Specify the screen capture plug-in to set up the video source.
  SrcVid.SetInput "ScreenCap://ScreenCapture1"

C++ Example

// Include libraries.
#include <windows.h>
#include <atlbase.h>
#include <comdef.h>
#include "C:\WMSDK\WMEncSDK9\include\wmencode.h"

// Define the screen capture properties.
#define WMSCRNCAP_CAPTUREWINDOW     CComBSTR("CaptureWindow")
#define WMSCRNCAP_WINDOWLEFT        CComBSTR("Left")
#define WMSCRNCAP_WINDOWTOP         CComBSTR("Top")
#define WMSCRNCAP_WINDOWRIGHT       CComBSTR("Right")
#define WMSCRNCAP_WINDOWBOTTOM      CComBSTR("Bottom")
#define WMSCRNCAP_FLASHRECT         CComBSTR("FlashRect")
#define WMSCRNCAP_ENTIRESCREEN      CComBSTR("Screen")
#define WMSCRNCAP_WINDOWTITLE       CComBSTR("WindowTitle")

// Declare variables.
HRESULT hr = S_OK;
IWMEncoder* pEncoder;
IWMEncSourceGroupCollection* pSrcGrpColl;
IWMEncSourceGroup* pSrcGrp;
IWMEncSource* pSrc;
IWMEncVideoSource2* pSrcVid;
IPropertyBag* pPropertyBag;
CComVariant varValue;

// Initialize the COM library and retrieve a pointer to an IWMEncoder interface.
hr = CoInitialize(NULL);
if ( SUCCEEDED( hr ) )
{
    hr = CoCreateInstance(CLSID_WMEncoder,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IWMEncoder,
        (void**) &pEncoder);
}

// Retrieve the source group collection.
if ( SUCCEEDED( hr ) )
{
    hr = pEncoder->get_SourceGroupCollection(&pSrcGrpColl);
}

// Add a source group to the collection.
if ( SUCCEEDED( hr ) )
{
    hr = pSrcGrpColl->Add(CComBSTR("SG_1"), &pSrcGrp);
}
if ( SUCCEEDED( hr ) )
{
    hr = pSrcGrp->AddSource(WMENC_VIDEO, &pSrc);
}

// Retrieve an IWMEncVideoSource2 pointer.
if ( SUCCEEDED( hr ) )
{
    hr = pSrc->QueryInterface(IID_IWMEncVideoSource2, (void**)&pSrcVid);
}

// Add a video source to the source group.
if ( SUCCEEDED( hr ) )
{
    hr = pSrcVid->SetInput(CComBSTR("ScreenCap://ScreenCapture1"));
}

// Retrieve a pointer to the property bag.
if ( SUCCEEDED( hr ) )
{
    hr = pSrcVid->QueryInterface(IID_IPropertyBag, (void**)&pPropertyBag);
}

// Set full screen capture.
{
    varValue = true;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_ENTIRESCREEN, &varValue );
    }
}

// Set the capture area.
{
    // nLeft, nRight, nTop, and nBottom are the dimensions to capture
    int nLeft, nRight, nTop, nBottom;

    // Initialize the capture area. The size must be even.
    varValue = false;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_ENTIRESCREEN, &varValue );
    }

    varValue = nLeft;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWLEFT, &varValue );
    }

    varValue = nRight;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWRIGHT, &varValue );
    }

    varValue = nTop;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWTOP, &varValue );
    }

    varValue = nBottom;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_WINDOWBOTTOM, &varValue );
    }

    varValue = true;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPropertyBag->Write( WMSCRNCAP_FLASHRECT, &varValue );
    }

    // Continue configuring the encoding session.

    // Release pointers.
    if ( pSrcGrpColl )
    {
        pSrcGrpColl->Release();
        pSrcGrpColl = NULL;
    }
    if ( pSrcGrp )
    {
        pSrcGrp->Release();
        pSrcGrp = NULL;
    }
    if ( pSrcVid )
    {
        pSrcVid->Release();
        pSrcVid = NULL;
    }
    if ( pSrc )
    {
        pSrc->Release();
        pSrc = NULL;
    }
    if ( pPropertyBag )
    {
        pPropertyBag->Release();
        pPropertyBag = NULL;
    }
    if ( pEncoder )
    {
        pEncoder->Release();
        pEncoder = NULL;
    }
}

See Also