C# 丰富的经验和文件处理

C# 丰富的经验和文件处理,c#,aforge,C#,Aforge,早上好 这里有没有人有使用谷歌、视频或网络摄像头应用程序的经验 有一个名为VideoCaptureDeviceForm的.cs文件,它是org.DirectShow的一部分,该文件提供了一种选择本地视频设备(即网络摄像头)的方法 我希望能够自动检测并启动计算机上的网络摄像头,但会弹出一个对话框,要求选择网络摄像头。我需要一种方法,要么以某种方式绕过这个,要么自动按下OK按钮 我曾尝试在打开对话框按钮方法(即启动摄像头)中使用SendKeys.Send({ENTER}),但出现错误 在Vide

早上好

这里有没有人有使用谷歌、视频或网络摄像头应用程序的经验

有一个名为VideoCaptureDeviceForm的.cs文件,它是org.DirectShow的一部分,该文件提供了一种选择本地视频设备(即网络摄像头)的方法

我希望能够自动检测并启动计算机上的网络摄像头,但会弹出一个对话框,要求选择网络摄像头。我需要一种方法,要么以某种方式绕过这个,要么自动按下OK按钮

我曾尝试在打开对话框按钮方法(即启动摄像头)中使用SendKeys.Send({ENTER}),但出现错误

在VideoCaptureDeviceForm.cs中,有一种称为okButton\u Click的方法,下一个想法是操纵它来完成我希望应用程序完成的任务

不幸的是,我没有经验来解决这个问题。有人能给我建议怎么做或者我下一步该怎么做吗?多谢各位

VideoCaptureDeviceForm.cs如下:

using System;
using System.Threading;
using System.Windows.Input;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using AForge.Video.VFW;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    private FilterInfoCollection VideoCaptureDevices;
    private VideoCaptureDevice FinalVideo = null;
    private VideoCaptureDeviceForm captureDevice;
    private Bitmap video;
    private VideoFileWriter FileWriter = new VideoFileWriter();
    private SaveFileDialog saveAvi;

    public Form1()
    {
        InitializeComponent();


        Start_Vid();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        captureDevice = new VideoCaptureDeviceForm();

    }

    void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        if (Stop.Text == "Stop Record")
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            //AVIwriter.Quality = 0;
            FileWriter.WriteVideoFrame(video);
            //AVIwriter.AddFrame(video);
        }
        else
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        }
    }

    private void Stop_Vid()
    {
        if (Stop.Text == "Stop Record")
        {
            Stop.Text = "Stop";
            if (FinalVideo == null)
            { return; }
            if (FinalVideo.IsRunning)
            {
                //this.FinalVideo.Stop();
                FileWriter.Close();
                //this.AVIwriter.Close();
                pictureBox1.Image = null;
            }
        }
        else
        {
            this.FinalVideo.Stop();
            FileWriter.Close();
            //this.AVIwriter.Close();
            pictureBox1.Image = null;
        }
    }
    private void butstop_Click(object sender, EventArgs e)
    {
        if (Stop.Text == "Stop Record")
        {
            Stop.Text = "Stop";
            if (FinalVideo == null)
            { return; }
            if (FinalVideo.IsRunning)
            {
                //this.FinalVideo.Stop();
                FileWriter.Close();
                //this.AVIwriter.Close();
                pictureBox1.Image = null;
            }
        }
        else
        {
            this.FinalVideo.Stop();
            FileWriter.Close();
            //this.AVIwriter.Close();
            pictureBox1.Image = null;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        pictureBox1.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (FinalVideo == null)
        { return; }
        if (FinalVideo.IsRunning)
        {
            this.FinalVideo.Stop();
            FileWriter.Close();
            //this.AVIwriter.Close();
        }
    }

    private void Save_Click(object sender, EventArgs e)
    {
        saveAvi = new SaveFileDialog();
        saveAvi.Filter = "Avi Files (*.avi)|*.avi";
        saveAvi.FileName = "New Vid1";

        if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
            int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
            FileWriter.Open(saveAvi.FileName, w, h, 25, VideoCodec.Default, 5000000);
            FileWriter.WriteVideoFrame(video);


            //AVIwriter.Open(saveAvi.FileName, w, h);
            Stop.Text = "Stop Record";
            //FinalVideo = captureDevice.VideoDevice;
            //FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            //FinalVideo.Start();
        }


    }

    //##############################################//
    //        Method not working        //
    //##############################################//

    private void Start_Vid()
    {
        SendKeys.Send("{ENTER}");
        if (captureDevice.ShowDialog(this) == DialogResult.OK)
        {

            VideoCaptureDevice videoSource = captureDevice.VideoDevice;
            FinalVideo = captureDevice.VideoDevice;
            FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            FinalVideo.Start();
        }
    }

    //##############################################//
    //        Method working            //
    //##############################################//

    private void Start_Click(object sender, EventArgs e)
    {
        SendKeys.Send("{ENTER}");
        if (captureDevice.ShowDialog(this) == DialogResult.OK)
        {
            VideoCaptureDevice videoSource = captureDevice.VideoDevice;
            FinalVideo = captureDevice.VideoDevice;
            FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            FinalVideo.Start();
        }
    }
    private void Close_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}

1)首先,您需要查找所有网络摄像头设备是否与您的系统连接

public FilterInfoCollection LoaclWebCamsCollection  = new FilterInfoCollection(FilterCategory.VideoInputDevice);
2) 然后选择要使用的网络摄像头,并选择该网络摄像头的“监视器字符串”

    VideoCaptureDevice videoSource = new VideoCaptureDevice(LoaclWebCamsCollection["Select webcam you want to user"].MonikerString);
                      OR
    VideoCaptureDevice videoSource = new VideoCaptureDevice(LoaclWebCamsCollection[0].MonikerString);




private void Start_Vid()
{
    FinalVideo = new 
    VideoCaptureDevice(LoaclWebCamsCollection[0].MonikerString);
    FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
    FinalVideo.Start();
}
1) 首先,您需要查找所有网络摄像头设备是否与您的系统连接

public FilterInfoCollection LoaclWebCamsCollection  = new FilterInfoCollection(FilterCategory.VideoInputDevice);
2) 然后选择要使用的网络摄像头,并选择该网络摄像头的“监视器字符串”

    VideoCaptureDevice videoSource = new VideoCaptureDevice(LoaclWebCamsCollection["Select webcam you want to user"].MonikerString);
                      OR
    VideoCaptureDevice videoSource = new VideoCaptureDevice(LoaclWebCamsCollection[0].MonikerString);




private void Start_Vid()
{
    FinalVideo = new 
    VideoCaptureDevice(LoaclWebCamsCollection[0].MonikerString);
    FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
    FinalVideo.Start();
}

请附上对话图片。。并在调用start webcam的位置显示您的代码..请细化/最小化代码,以便我们了解问题点..嗨,Ankur,我已经添加了对话框,并用我的应用程序代码替换了VideoCaptureDeviceForm.cs。我已经包括了所有问题,但问题在于private void Start_Vid()。我假设有SendKeys.Send({ENTER});在if语句之前,它将起作用。在开始之前,您需要先选择网络摄像头。。我添加了答案检查请附上对话图片。。并在调用start webcam的位置显示您的代码..请细化/最小化代码,以便我们了解问题点..嗨,Ankur,我已经添加了对话框,并用我的应用程序代码替换了VideoCaptureDeviceForm.cs。我已经包括了所有问题,但问题在于private void Start_Vid()。我假设有SendKeys.Send({ENTER});在if语句之前,它将起作用。在开始之前,您需要先选择网络摄像头。。我添加了答案checkoutHi Ankur,在我的应用程序中什么时候我会把这个代码放进去?此外,这将自动找到网络摄像头,并启动它,因为我只有一个网络摄像头是内置在我的笔记本电脑。我的应用程序的想法是能够在网络摄像头应该打开时进行编程,而无需用户干预即可按OK。在启动方法中使用此代码,只需创建VideoCaptureDevice对象并传递网络摄像头的MonitorString,然后启动,因此我已将上述代码放入start_Vid()并运行它。第二行给出了错误“无法将非可开票成员“VideoCaptureDevice”用作方法。有什么想法吗?你能把有错误的图片贴上去吗?你从哪里得到的错误?您使用的是哪种版本的Forge?一个大api的:-嗨,安克尔,我已经附上了我的问题的错误。我正在使用一个RGE版本3.8.0.Hi Ankur,在我的应用程序中什么时候我会把这个代码放进去?此外,这将自动找到网络摄像头,并启动它,因为我只有一个网络摄像头是内置在我的笔记本电脑。我的应用程序的想法是能够在网络摄像头应该打开时进行编程,而无需用户干预即可按OK。在启动方法中使用此代码,只需创建VideoCaptureDevice对象并传递网络摄像头的MonitorString,然后启动,因此我已将上述代码放入start_Vid()并运行它。第二行给出了错误“非发票会员‘VideoCaptureDevice’不能像使用方法一样使用。有什么想法吗?你能附加有错误的图像吗?你从哪里得到错误?你使用的是哪个版本的Forge?Forge api:-嗨,Ankur,我已将错误附加到我的问题中。我使用的是Forge版本3.8.0。