NewTOAPIA.DirectShow.VideoCaptureDevice.ConfigureSourceFilter C# (CSharp) Method

ConfigureSourceFilter() private method

Configure the capture filter once it is in the graph.
We configure the source filter with the desired size, mediasubtype, and bits per pixel. This must be done before the capture pin is connected to other filters in the graph.
private ConfigureSourceFilter ( IAMStreamConfig streamConfig, int iWidth, int iHeight, short bitsPerPixel ) : void
streamConfig IAMStreamConfig
iWidth int Desired width of video
iHeight int Desired height of video
bitsPerPixel short Number of bits per pixel
return void
        private void ConfigureSourceFilter(IAMStreamConfig streamConfig, int iWidth, int iHeight, short bitsPerPixel)
        {
            int hr;
            AMMediaType mType = new AMMediaType();
            VIDEOINFOHEADER v;

            // First, we need to get the existing format block
            // If there's an error, throw an exception.
            hr = streamConfig.GetFormat(out mType);
            DsError.ThrowExceptionForHR(hr);

            // Return early if the media type is not what we expect
            // First, the majorType must be 'Video'
            // Second, the size of the format structure must be sizeof(VIDEOINFOHEADER)
            if ((MediaType.Video != mType.majorType) || 
                (mType.formatSize != Marshal.SizeOf(typeof(VIDEOINFOHEADER))))
                return;

            try
            {
                // The formatPtr member of the AMMediaType object points
                // to a chunk of unmanaged memory that contains the data
                // for the format structure.  We need to copy this data 
                // into a structure object so we can manipulate it.
                v = new VIDEOINFOHEADER();
                Marshal.PtrToStructure(mType.formatPtr, v);

                // In order to change the size and bits per pixel, we
                // need to change the values of the VIDEOINFOHEADER object
                // that we pulled out from the AMMediaType object.
                if (iWidth > 0)
                {
                    v.BmiHeader.biWidth = iWidth;
                }

                if (iHeight > 0)
                {
                    v.BmiHeader.biHeight = iHeight;
                }

                if (bitsPerPixel > 0)
                {
                    v.BmiHeader.biBitCount = (ushort)bitsPerPixel;
                }

                // Now we copy the VIDEOINFOHEADER structure back into 
                // a piece of unmanaged memory and give the pointer to 
                // the AMMediaType structure.
                Marshal.StructureToPtr(v, mType.formatPtr, false);

                // Finally, we set the new format on the pin
                hr = streamConfig.SetFormat(mType);
                DsError.ThrowExceptionForHR(hr);
            }
            finally
            {
                mType.Dispose();
            }
        }