要使用 DirectShow 调用摄像头录像,首先需要添加 DirectShow 库的引用。可以在 Visual Studio 中右键单击项目,选择'添加引用',然后在 COM 选项卡中找到'DirectShow'并添加。

以下是一个简单的示例代码,用于通过 DirectShow 调用摄像头录像:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DirectShowLib;

namespace WebcamRecording
{
    public partial class MainForm : Form
    {
        private IFilterGraph2 filterGraph;
        private ICaptureGraphBuilder2 captureGraphBuilder;
        private IBaseFilter videoCaptureFilter;
        private IBaseFilter muxFilter;
        private IFileSinkFilter fileSinkFilter;
        private ICaptureGraphBuilder2 graphBuilder;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 创建 FilterGraph
            filterGraph = (IFilterGraph2)new FilterGraph();
            captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

            // 添加视频捕获设备
            Guid videoCaptureCategory = FilterCategory.VideoInputDevice;
            Guid videoCaptureGuid;
            DsDevice[] videoCaptureDevices = DsDevice.GetDevicesOfCat(videoCaptureCategory);
            if (videoCaptureDevices.Length > 0)
            {
                videoCaptureGuid = videoCaptureDevices[0].DevicePath;
            }
            else
            {
                MessageBox.Show('No video capture device found.');
                return;
            }

            int hr = filterGraph.AddSourceFilterForMoniker(videoCaptureDevices[0].Mon, null, 'Video Capture', out videoCaptureFilter);
            DsError.ThrowExceptionForHR(hr);

            // 创建文件写入过滤器
            muxFilter = (IBaseFilter)new AVI_Mux();
            hr = filterGraph.AddFilter(muxFilter, 'AVI Mux');
            DsError.ThrowExceptionForHR(hr);

            // 创建 FileSinkFilter
            fileSinkFilter = (IFileSinkFilter)muxFilter;
            hr = fileSinkFilter.SetFileName('C:\path\to\output.avi', null);
            DsError.ThrowExceptionForHR(hr);

            // 连接视频捕获设备和文件写入过滤器
            hr = captureGraphBuilder.SetFiltergraph(filterGraph);
            DsError.ThrowExceptionForHR(hr);
            hr = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, videoCaptureFilter, null, muxFilter);
            DsError.ThrowExceptionForHR(hr);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            // 开始录制
            int hr = captureGraphBuilder.ControlStream(PinCategory.Capture, MediaType.Video, videoCaptureFilter, null, 0, 0);
            DsError.ThrowExceptionForHR(hr);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            // 停止录制
            int hr = captureGraphBuilder.ControlStream(PinCategory.Capture, MediaType.Video, videoCaptureFilter, null, 1, 0);
            DsError.ThrowExceptionForHR(hr);

            // 释放资源
            Marshal.ReleaseComObject(fileSinkFilter);
            Marshal.ReleaseComObject(muxFilter);
            Marshal.ReleaseComObject(videoCaptureFilter);
            Marshal.ReleaseComObject(captureGraphBuilder);
            Marshal.ReleaseComObject(filterGraph);
        }
    }
}

请确保将'C:\path\to\output.avi'替换为实际的输出文件路径。然后,您可以在按钮的点击事件中调用btnStart_Click开始录制,调用btnStop_Click停止录制。

这只是一个简单的示例,如果您想要更多的控制和功能,您可能需要进一步了解 DirectShow 的相关知识。

C# 使用 DirectShow 调用摄像头进行录像

原文地址: https://www.cveoy.top/t/topic/qqBL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录