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

发布于 2024-10-06 18:21:25 字数 2008 浏览 0 评论 0原文

我正在尝试使用 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 位,因此我无法更改网络摄像头的分辨率捕获,任何人都知道更好的方法来做到这一点或如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-10-13 18:21:25

确保在查找 HR 错误代码时使用 DirectShow 错误和成功代码列表,而不是通用 HR 代码列表。您将从该列表中看到 0x80040209 的实际含义是:

VFW_E_NOT_CONNECTED 由于引脚未连接,无法执行操作。

看起来您的图表没有连接您的样本采集器过滤器。确保将调用中的示例抓取器传递给 RenderStream

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.

似梦非梦 2024-10-13 18:21:25

更改网络摄像头分辨率的正确方法是查询其输出引脚 IAMStreamConfig 接口。它可以列出可用的格式和分辨率并选择一种。如果您运行 GraphEditPlus 并右键单击相机滤镜的输出引脚,您将能够看到该列表并选择分辨率。然后您可以生成代码(通过“文件”菜单)并查看它是如何在 C# 中完成的。

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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文