Commented code does not initialize videoInfoHeader
correctly. You need to initialize all members there (well, some might be left with zeros, but you have to add values for mandatory ones). biCompression
, biBitCount
to say the least. Also your code does not even initialize those members and vice versa reads uninitialized values back.
This is however a wrong way already. Most samples suggest that you don't intialize format
and formatPtr
for a reason. With major type and subtype, Sample Grabber would "hint" intelligent connect what format you want data in (24-bit RGB here and typically). Yes this is what you can do and this works out well. However there is no flexibility to specify resolution there, or frame rate, not even every pixel format works out. That is, whatever you are trying to do here is likely to be incorrect. You are supposed to be happy with partial media type (major type and subtype only).
DxScan
from DirectShow.NET Samples adds the Sample Grabber and shows how to do it and how to set it up:
private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
AMMediaType media;
int hr;
// Set the media type to Video/RBG24
media = new AMMediaType();
media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo;
hr = sampGrabber.SetMediaType( media );
DsError.ThrowExceptionForHR( hr );
DsUtils.FreeAMMediaType(media);
media = null;
// Choose to call BufferCB instead of SampleCB
hr = sampGrabber.SetCallback( this, 1 );
DsError.ThrowExceptionForHR( hr );
}