Resizing and Cropping Video

Windows Media Encoder SDK banner art

When you encode video content, the profile you use determines what the output size of the video will be. You can modify the video height and width settings for each audience in the profile by using IWMEncAudienceObj. You can also modify the profile to use the same dimensions as the video input. For more information, see Creating and Modifying Profiles.

You can also crop the edges of a video image to change its size. For example, noise often occurs at the edges of an image, so you can crop one or more edges to eliminate the affected rows. You can specify how much to crop from the top, bottom, left, and right edges of an image by using IWMEncVideoSource.

Note   When cropping content that is being deinterlaced or to which you are applying inverse telecine, the top and bottom cropping values must be even numbers.

If you are capturing a live video image and you are also cropping it, you can specify the final height and width of the video using IWMEncVideoSource2 to protect the integrity of the video image. For example, rather than cropping the edges from a live image and then stretching the image to fit the output size, a larger image is captured and then cropped down to the output size. Using IWMEncVideoSource2 also compensates for any problems that occur when cropping images from capture devices that provide video input at one fixed size or with video optimization (such as deinterlacing or telecine).

The following examples show how to crop a video image. For end-to-end code examples, see Complete Code Examples.

Visual Basic Example

' Create a WMEncoder object.
  Dim Encoder As WMEncoder
  Set Encoder = New WMEncoder

' Specify the source for the input stream.
  Dim SrcGrpColl As IWMEncSourceGroupCollection
  Dim SrcGrp As IWMEncSourceGroup
  Dim SrcVid As IWMEncVideoSource
  Dim SrcAud As IWMEncSource

' Add a source group to the collection.
  Set SrcGrpColl = Encoder.SourceGroupCollection
  Set SrcGrp = SrcGrpColl.Add("SG_1")

' Add a video and audio source to the source group.
  Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO)
  Set SrcAud = SrcGrp.AddSource(WMENC_AUDIO)

' Specify the cropping margins.
  SrcVid.CroppingBottomMargin = 5
  SrcVid.CroppingTopMargin = 5
  SrcVid.CroppingLeftMargin = 3
  SrcVid.CroppingRightMargin = 3

C++ Example

// Include libraries.
#include <windows.h>
#include "wmencode.h"

// Declare variables.
    HRESULT hr;
    IWMEncoder* pEncoder;
    IWMEncSourceGroupCollection* pSrcGrpColl;
    IWMEncSourceGroup* pSrcGrp;
    IWMEncSource* pAudSrc;
    IWMEncSource* pSrc;
    IWMEncVideoSource* pVidSrc;

// 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 an IWMEncSourceGroupCollection pointer.
    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);
    }

// Add a video stream to the source group.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrp->AddSource(WMENC_VIDEO, &pSrc);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrp->AddSource(WMENC_AUDIO, &pAudSrc);
    }

// Retrieve an IWMEncVideoSource pointer.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrc->QueryInterface(IID_IWMEncVideoSource, (void**)&pVidSrc);
    }

// Specify the cropping margins.
    if ( SUCCEEDED( hr ) )
    {
        hr = pVidSrc->put_CroppingBottomMargin(5);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pVidSrc->put_CroppingTopMargin(5);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pVidSrc->put_CroppingLeftMargin(3);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pVidSrc->put_CroppingRightMargin(3);
    }

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

See Also