/// <summary> /// Configure the capture filter once it is in the graph. /// </summary> /// <param name="streamConfig"></param> /// <param name="iWidth">Desired width of video</param> /// <param name="iHeight">Desired height of video</param> /// <param name="bitsPerPixel">Number of bits per pixel</param> /// <remarks> /// 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. /// </remarks> 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(); } }
void GetCaptureSize() { int hr = 0; // After the filter is added to the graph, and before // it is connected to anything, we need to set the format // and size that we expect. AMMediaType mt = new AMMediaType(); hr = sampleGrabber.GetConnectedMediaType(mt); DsError.ThrowExceptionForHR(hr); try { if ((mt.formatType != FormatType.VideoInfo) || (mt.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Get the struct VIDEOINFOHEADER videoInfoHeader = new VIDEOINFOHEADER(); Marshal.PtrToStructure(mt.formatPtr, videoInfoHeader); // Grab the size info fGrabber.Width = videoInfoHeader.BmiHeader.biWidth; fGrabber.Height = videoInfoHeader.BmiHeader.biHeight; fGrabber.FrameBytes = (int)videoInfoHeader.BmiHeader.biSizeImage; } finally { mt.Dispose(); } }
void AddSampleGrabber() { int hr = 0; // Add the sample grabber to the graph hr = graphBuilder.AddFilter(grabberFilter, "NewTOAPIA Grabber"); DsError.ThrowExceptionForHR(hr); // After the filter is added to the graph, and before // it is connected to anything, we need to set the format // and size that we expect. AMMediaType mt = new AMMediaType(); // Set the media type to Video/RBG24 mt.majorType = MediaType.Video; mt.subType = MediaSubType.RGB24; mt.formatType = FormatType.VideoInfo; // What format do we want to use to describe the media //VIDEOINFOHEADER vih = new VIDEOINFOHEADER(); //vih.BmiHeader.biBitCount = 24; //vih.BmiHeader.biClrImportant = 0; //vih.BmiHeader.biClrUsed = 0; //vih.BmiHeader.biCompression = 0; //vih.BmiHeader.biHeight = fDesiredHeight; //vih.BmiHeader.biPlanes = 1; //vih.BmiHeader.biSize = Marshal.SizeOf(vih); //vih.BmiHeader.biSizeImage = (uint)(fDesiredWidth * fDesiredHeight * 3); // It's very important to set this one or things won't be right //vih.BmiHeader.biWidth = fDesiredWidth; //vih.BmiHeader.biXPelsPerMeter = 0; //vih.BmiHeader.biYPelsPerMeter = 0; //// turn the structure back into a pointer //mt.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(vih)); //mt.formatSize = Marshal.SizeOf(vih); //mt.formatType = FormatType.VideoInfo; //Marshal.StructureToPtr(vih, mt.formatPtr, false); hr = sampleGrabber.SetMediaType(mt); DsError.ThrowExceptionForHR(hr); // Free the media type object because we're done with it mt.Dispose(); mt = null; // Final sample grabber configuration hr = sampleGrabber.SetOneShot(false); hr = sampleGrabber.SetBufferSamples(false); hr = sampleGrabber.SetCallback(fGrabber, 1); DsError.ThrowExceptionForHR(hr); }