Question

I am trying to figure out, how can i get an bitmap data from a filter. I am using DirectShowNet wrapper to get an image from my webcamera.

My current code is:

public partial class Form1 : Form
{
    public IGraphBuilder gb;
    public ICaptureGraphBuilder2 cgb;
    public IBaseFilter filter;

    public Form1()
    {
        InitializeComponent();

        DsDevice[] videoInputDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);            
        object obj = null; Guid iid = typeof(IBaseFilter).GUID;
        videoInputDevices[1].Mon.BindToObject(null, null, ref iid, out obj);
        filter = (IBaseFilter)obj;

        ((IAMCameraControl)filter).Set(CameraControlProperty.Exposure, 0, CameraControlFlags.Auto);

        gb  = (IGraphBuilder) new FilterGraph();
        cgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

        cgb.SetFiltergraph(gb);

        gb.AddFilter(filter, "First Filter");                        

        cgb.RenderStream(PinCategory.Preview, MediaType.Video, filter, null, null);
        ((IVideoWindow)gb).put_Owner(this.panel1.Handle);
        ((IVideoWindow)gb).put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
        ((IVideoWindow)gb).put_Visible(OABool.True);
        ((IVideoWindow)gb).SetWindowPosition(0, 0, this.panel1.Width, this.panel1.Height);
        ((IMediaControl)gb).Run();
    }
}

This simple code just render webcamera output to panel control. I tried to use timer and SaveToBitmap function to copy image from panel to bitmap, but bitmap is blank after that.

I read something about Grabber filter, but my solution did not work, it returned null ptr to buffer/sample.

I would like to ask, what should i add to be able to read image data ? Thank you very much.

Was it helpful?

Solution

Standard behavior of DirectShow pipeline is such that filters pass data one to another without showing it to the controlling application and code, so there is no direct way to access the data.

You typically do one of the following:

  1. You add Sample Grabber Filter to certain position of your pipeline and set it up so that SG calls you back every time it has data going through
  2. You grab a copy of currently displayed video from video renderer

Both methods are documented, popular and discussed multiple times including on StackOverflow:

OTHER TIPS

Here's a detailed example of exactly this:

Working with raw video data from webcam in C# and DirectShowNet

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top