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?


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 .


The right way to change web camera resolution is to query its output pin IAMStreamConfig interface. It can list available formats and resolution and choose one. If you run GraphEditPlus and right click the camera filter's output pin, you'll be able to see that list and make a choice of resolution. Then you can generate code (via File menu) and see how it's done in C#.

链接地址: http://www.djcxy.com/p/80990.html

上一篇: 使用网络摄像头捕获视频并使用c#保存为avi

下一篇: 无法使用c#更改视频捕获分辨率