以下是一个简单的C#程序,可以调用摄像头录制视频。

需要注意的是,需要引用System.Windows.Forms和System.Drawing命名空间。

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

namespace CameraRecord
{
    public partial class Form1 : Form
    {
        private IntPtr hCapWnd;
        private IntPtr hCapDrv;

        [DllImport("avicap32.dll")]
        public static extern IntPtr capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);

        [DllImport("avicap32.dll")]
        public static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer);

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

        private const int WM_CAP_START = 0x400;
        private const int WM_CAP_DRIVER_CONNECT = 0x40a;
        private const int WM_CAP_SET_PREVIEW = 0x432;
        private const int WM_CAP_SET_PREVIEWRATE = 0x434;
        private const int WM_CAP_SEQUENCE = 0x410;
        private const int WM_CAP_FILE_SAVEAS = 0x442;

        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x40;

        private const int IDCANCEL = 2;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            hCapWnd = capCreateCaptureWindowA("Capture", WS_VISIBLE | WS_CHILD, 0, 0, 320, 240, this.Handle, 0);
            if (hCapWnd != IntPtr.Zero)
            {
                SendMessage(hCapWnd, WM_CAP_DRIVER_CONNECT, 0, 0);
                SendMessage(hCapWnd, WM_CAP_SET_PREVIEWRATE, 30, 0);
                SendMessage(hCapWnd, WM_CAP_SET_PREVIEW, 1, 0);
            }
        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            if (hCapWnd != IntPtr.Zero)
            {
                SendMessage(hCapWnd, WM_CAP_SEQUENCE, 0, 0);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (hCapWnd != IntPtr.Zero)
            {
                SendMessage(hCapWnd, WM_CAP_STOP, 0, 0);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (hCapWnd != IntPtr.Zero)
            {
                SendMessage(hCapWnd, WM_CAP_FILE_SAVEAS, 0, 0);
            }
        }
    }
}

在表单的Load事件中,我们使用capCreateCaptureWindowA函数创建一个名为"Capture"的窗口,并将其设置为可见和子窗口,然后将其与当前的表单句柄关联。接下来,我们使用capGetDriverDescriptionA函数获取系统上的摄像头驱动程序,并使用SendMessage函数连接到驱动程序。

在录制按钮的Click事件中,我们使用SendMessage函数发送WM_CAP_SEQUENCE消息,开始录制视频。在停止按钮的Click事件中,我们使用SendMessage函数发送WM_CAP_STOP消息,停止录制视频。在保存按钮的Click事件中,我们使用SendMessage函数发送WM_CAP_FILE_SAVEAS消息,将录制的视频保存到指定的文件中。

注意,此程序使用的是avicap32.dll库,该库在较新的Windows版本上可能已过时。因此,在实际使用时,您可能需要使用其他库或API

用C#写一个调用摄像头录像的程序

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

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