编程语言
首页 > 编程语言> > 无法使用C#更改视频捕获分辨率

无法使用C#更改视频捕获分辨率

作者:互联网

我正在尝试使用C#中的DirectShowNet更改默认的网络摄像头分辨率,根据我的收集,我需要通过调用Windows win32 api dll中的内置VideoInfoHeader类进行AVI捕获来更改它.我从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;

问题是我无法访问videoInfoHeader,因为在这一行:hr = sampGrabber.GetConnectedMediaType(media);
它会说hr小于0,因此引发此错误:接口有太多方法可以触发事件(HRESULT的异常:0x80040209)

它不会读取VideoInfoHeader位,因此我无法更改网络摄像头捕获的分辨率,有人知道更好的方法或解决此问题的方法吗?

解决方法:

确保在查找HR错误代码时使用的是DirectShow Error and Success Code list,而不是常规HR代码列表.您将从该列表中看到0x80040209的实际含义是:

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

看起来您的图形未连接样品采集器过滤器.确保将样品采集卡传递给RenderStream.

标签:capture,video-capture,webcam,directshow,c
来源: https://codeday.me/bug/20191208/2095015.html