title: How to get video frame using DirectShow.net?
body: <p>As part of a program I have to be able to pull a few random frames from a user specified video file. I do not have any experience with video processing programming, so this is new to me.</p>
  
  <p>I've determined that I need to use DirectShow.net to do this on Windows XP using .Net, but I cannot figure out how to get it done. I'm trying to use the ISampleGrabber interface but I can't get it all figured out and can't find any examples.</p>
  
  <p>There are very few examples of DirectShow.net usage on the internet, but surely someone has pulled frames from a video using .net, so I'm just looking to see if anyone knows of sample code that does this or something close.</p>
  
  <p>Trying to follow along with this code project article I've come up with this code:</p>
  
  <pre><code>// step 1
  IGraphBuilder graphBuilder = (IGraphBuilder)new FilterGraph();
  
  // step 2
  IBaseFilter grabberBaseFilter;
  ISampleGrabber sampleGrabber;
  AMMediaType mt;
  grabberBaseFilter = (IBaseFilter)new SampleGrabber();
  sampleGrabber = (ISampleGrabber)grabberBaseFilter;
  graphBuilder.AddFilter(grabberBaseFilter, "Grabber");
  
  // step 3
  
  mt = new AMMediaType();
  mt.majorType = MediaType.Video;
  mt.subType = MediaSubType.RGB24;
  mt.formatType = FormatType.VideoInfo;
  sampleGrabber.SetMediaType(mt);
  graphBuilder.RenderFile(filename, null);
  
  IMediaControl mediaControl = (IMediaControl)graphBuilder;
  IMediaEvent mediaEventEx = (IMediaEvent)graphBuilder;
  
  sampleGrabber.SetBufferSamples(true);
  sampleGrabber.SetOneShot(true);
  
  // step 4
  
  mediaControl.Run();
  
  EventCode evCode;
  mediaEventEx.WaitForCompletion(int.MaxValue, out evCode);
  
  AMMediaType connectedMediaType = new AMMediaType();
  sampleGrabber.GetConnectedMediaType(connectedMediaType);
  
  VideoInfoHeader videoHeader = (VideoInfoHeader)connectedMediaType.formatType;
  
  BitmapInfo bitmapInfo = new BitmapInfo();
  bitmapInfo.bmiHeader = videoHeader.BmiHeader;
  
  // ::CreateDIBSection(0, &amp;BitmapInfo, DIB_RGB_COLORS, &amp;buffer, NULL, 0); 
  // legacy - GdiFlush(); 
  
  // Copy the image into the buffer. 
  int size = 0;
  sampleGrabber.GetCurrentBuffer(ref size, buffer);
  </code></pre>
  
  <p>The part that wont compile is the videoHeader cast line and the GetCurrentBuffer line at the end because there is no buffer variable... but I'm not sure what to do to fix it.</p>