按钮点击时如何使用图片框弹出相机?

时间:2013-04-30 13:05:50

标签: c# camera directshow

我在C#窗口中创建一个程序我在我的表单中有很多功能,包括连接到dabase的两个datagrid视图,包括一个直接连接到我的PC的摄像头我使用AForge dll引用连接到摄像头设备我只是在youtube找到了这个教程,它对我来说很完美,正如我之前所说,我有一个包括那个摄像头在内的一个表格中有太多的程序,而且需要将摄像头调整为小分辨率,所以我当我点击表格上的按钮时,我决定制作一个必须显示更宽分辨率的弹出按钮。

这是我相机的代码。

    //Camera

    // get the devices name
    private void getCamList()
    {
        try
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            comboBox1.Items.Clear();
            if (videoDevices.Count == 0)
                throw new ApplicationException();

            DeviceExist = true;
            foreach (FilterInfo device in videoDevices)
            {
                comboBox1.Items.Add(device.Name);
            }
            comboBox1.SelectedIndex = 0; //make dafault to first cam
        }
        catch (ApplicationException)
        {
            DeviceExist = false;
            comboBox1.Items.Add("No capture device on your system");
        }
    }

    //refresh button
    private void refresh_Click(object sender, EventArgs e)
    {
        getCamList();
    }

    //toggle start and stop button
    private void start_Click(object sender, EventArgs e)
    {
        if (start.Text == "&Start")
        {
            if (DeviceExist)
            {
                videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                CloseVideoSource();
                videoSource.DesiredFrameSize = new Size(160, 120);
                //videoSource.DesiredFrameRate = 10;
                videoSource.Start();
                lblCam.Text = "Device running...";
                start.Text = "&Stop";
            }
            else
            {
                lblCam.Text = "Error: No Device selected.";
            }
        }
        else
        {
            if (videoSource.IsRunning)
            {
                CloseVideoSource();
                lblCam.Text = "Device stopped.";
                start.Text = "&Start";
            }
        }
    }

    //eventhandler if new frame is ready
    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap img = (Bitmap)eventArgs.Frame.Clone();
        //do processing here
        pictureBox1.Image = img;
    }

    //close the device safely
    private void CloseVideoSource()
    {
        if (!(videoSource == null))
            if (videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource = null;
            }
    }

    //prevent sudden close while device is running
    private void Form1_FormClosed(object sender, FormClosingEventArgs e)
    {
        CloseVideoSource();
    }


} }

我还张贴了一张照片,以便您进一步了解我在说什么。 enter image description here 你可以在右下角看到我有一个弹出按钮,老实说,我已经尝试了不同的方法,但没有任何工作,不幸的是我无法发布我尝试过,因为我昨天创建它,不能再撤消我试过的代码。任何的想法?

1 个答案:

答案 0 :(得分:0)

  • 创建新表单
  • 在此表单上放置PictureBox
  • 添加所有方法以初始化并将当前帧提供给表单(应该重构为自己的类,提供类似FrameChanged的事件,为您提供当前图像)
  • 添加类似于表单的方法
    public void ShowCamPopup(string deviceName)
    {
      InitializeDevice(string deviceName, int width, int height);
      this.Show();
      this.BringToTop();
    }
    

你应该真的考虑将你的凸轮与你自己的类进行重构,以减少重复的代码,并允许你在解决方案的单个位置进行性能调整(以后肯定需要)。