Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change video capture resolution using c#

I am trying to change the default webcam resolution using DirectShowNet in C#, from what I gather I need to change it from calling the built in VideoInfoHeader class in windows win32 api dll for avi capture. I have the following code from DirectShowNet:

        hr = capGraph.SetFiltergraph( graphBuilder );
        if( hr < 0 )
            Marshal.ThrowExceptionForHR( hr );

        AMMediaType media = new AMMediaType();
        media.majorType = MediaType.Video;
        media.subType = MediaSubType.RGB24;
        media.formatType = FormatType.VideoInfo;        // ???
        hr = sampGrabber.SetMediaType(media);
        if (hr < 0)
            Marshal.ThrowExceptionForHR(hr);

        hr = graphBuilder.AddFilter( capFilter, "Ds.NET Video Capture Device" );
        if( hr < 0 )
            Marshal.ThrowExceptionForHR( hr );

        DsUtils.ShowCapPinDialog( capGraph, capFilter, this.Handle );

        Guid sub = MediaSubType.Avi;
        hr = capGraph.SetOutputFileName( ref sub, fileName, out mux, out sink );
        if( hr < 0 )
            Marshal.ThrowExceptionForHR( hr );

        Guid cat = PinCategory.Capture;
        Guid med = MediaType.Video;
        hr = capGraph.RenderStream( ref cat, ref med, capFilter, null, mux ); // stream to file 
        if( hr < 0 )
            Marshal.ThrowExceptionForHR( hr );


        media = new AMMediaType();
        hr = sampGrabber.GetConnectedMediaType(media);
        if (hr < 0)
            Marshal.ThrowExceptionForHR(hr);
        if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
            throw new NotSupportedException("Unknown Grabber Media Format");

        videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
        Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

Thing is I can't access videoInfoHeader because at this line: hr = sampGrabber.GetConnectedMediaType(media); it goes and says hr is less than 0 so throws this error: An interface has too many methods to fire events from (Exception from HRESULT: 0x80040209)

It won't read the VideoInfoHeader bit so I can't change the resolution of the webcam capture, anyone know a better way to do this or how to fix this?

like image 659
James Avatar asked Dec 05 '25 15:12

James


1 Answers

Make sure that when you look up your HR error codes you look them up using the DirectShow Error and Success Code list, not the generic HR code list. You'll see from that list that the actual meaning of 0x80040209 is:

VFW_E_NOT_CONNECTED The operation cannot be performed because the pins are not connected.

Looks like your graph is not connecting your sample grabber filter. Make sure to pass the sample grabber in your call to RenderStream.

like image 103
heavyd Avatar answered Dec 08 '25 04:12

heavyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!