Aforge练习

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;
namespace 调用Aforge
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoDevice;
private VideoCapabilities[] videoCapbilities;
private VideoCapabilities[] snapshotCapabilities;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        if (videoDevices.Count != 0)
        {
            foreach(FilterInfo device in videoDevices)
            {
                cmbCamera.Items.Add(device.Name);
            }

        }
        else
        {
            cmbCamera.Items.Add("没有找到摄像头");

        }
        cmbCamera.SelectedIndex = 0;

    }

    private void cmbCamera_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (videoDevices.Count != 0)
        {
            videoDevice = new VideoCaptureDevice(videoDevices[cmbCamera.SelectedIndex].MonikerString);
            GetDeviceResolution(videoDevice);

        }
    }

    private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
    {
        cmbResolution.Items.Clear();
        videoCapbilities = videoCaptureDevice.VideoCapabilities;
        foreach(VideoCapabilities capabilty in videoCapbilities)
        {
            cmbResolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height} y");

        }
        cmbResolution.SelectedIndex = 0;

        //throw new NotImplementedException();
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {
        if(videoDevice!=null)
        {
            if ((videoCapbilities != null) && (videoCapbilities.Length != 0))
            {
                videoDevice.VideoResolution = videoCapbilities[cmbResolution.SelectedIndex];
                vispShoot.VideoSource = videoDevice;
                vispShoot.Start();
                EnableControlStatus(false);
            }
        }
    }

    private void EnableControlStatus(bool status)
    {
        cmbCamera.Enabled = status;
        cmbResolution.Enabled = status;
        btnConnect.Enabled = status;
        btnShoot.Enabled = !status;
        btnDisconnect.Enabled = !status;

       // throw new NotImplementedException();
    }

    private void btnDisconnect_Click(object sender, EventArgs e)
    {
        Disconnect();
        EnableControlStatus(true);
    }

    private void Disconnect()
    {
        if (vispShoot.VideoSource != null)
        {
            vispShoot.SignalToStop();
            vispShoot.WaitForStop();
            vispShoot.VideoSource = null;
        }
        //throw new NotImplementedException();
    }

    private void btnShoot_Click(object sender, EventArgs e)
    {
        Bitmap img = vispShoot.GetCurrentVideoFrame();
        picbPreview.Image = img;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Disconnect();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值