Image Capture via Webcam C#

Asked

Viewed 797 times

0

I have an application developed with Windows Form in C# that captures image through webcam installed on the computer.

This application has several photons, and one of them is the image capture. When I finish capturing the image and close the form the webcam is still on. The form I’m closing is just the capture, the application keeps running. I want to stop the webcam and keep running the application.

using ColetaBiometria.DAO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ColetaBiometria
{
    public partial class CapturaFoto : Form
    {
        public DirectX.Capture.Filter Camera;
        public DirectX.Capture.Capture CaptureInfo;
        public DirectX.Capture.Filters CamContainer;
        Image capturaImagem;

        public CapturaFoto(int codEmpresa, string login)
        {
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(formClosing);
            labelNomeUsuario.Text = login;
            labelCodEmpresa.Text = Convert.ToString(codEmpresa);
        }

        private void CapturaFoto_Load(object sender, EventArgs e)
        {
            try
            {
                CamContainer = new DirectX.Capture.Filters();
            }
            catch
            {
                MessageBox.Show("WebCam não encontrada!" + "\n" + "\n" + "Verifique se o dispositivo está conectado.", "Alerta!");
            }


            try
            {
                int no_of_cam = CamContainer.VideoInputDevices.Count;
                for (int i = 0; i < no_of_cam; i++)
                {
                    try
                    {
                        // obtém o dispositivo de entrada do vídeo
                        Camera = CamContainer.VideoInputDevices[i];
                        // inicializa a Captura usando o dispositivo
                        CaptureInfo = new DirectX.Capture.Capture(Camera, null);
                        // Define a janela de visualização do vídeo
                        CaptureInfo.PreviewWindow = this.picWebCam;
                        // Capturando o tratamento de evento
                        CaptureInfo.FrameCaptureComplete += AtualizaImagem;
                        // Captura o frame do dispositivo
                        CaptureInfo.CaptureFrame();
                        // Se o dispositivo foi encontrado e inicializado então sai sem checar o resto
                        break;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(this, ex.Message);
            }
        }

        public void AtualizaImagem(PictureBox frame)
        {
            try
            {
                capturaImagem = frame.Image;
                this.picImagem.Image = capturaImagem;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro " + ex.Message);
            }
        }

        private void bntCapture_Click(object sender, EventArgs e)
        {
            try
            {
                CaptureInfo.CaptureFrame();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro " + ex.Message);
            }
        }

        private void formClosing(object sender, EventArgs e)
        {
            //Aqui estou tentanto encerrar o serviço da webcam
            CaptureInfo.Stop();
            CaptureInfo.Close();
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (picImagem.Image != null)
                {
                    MemoryStream ms = new MemoryStream();
                    picImagem.Image.Save(ms, ImageFormat.Jpeg);
                    byte[] photo_aray = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(photo_aray, 0, photo_aray.Length);

                    UsuarioDAO dao = new UsuarioDAO();
                    dao.GravaFoto(photo_aray, Convert.ToInt32(labelCodEmpresa.Text), labelNomeUsuario.Text);

                    MessageBox.Show("Foto gravada com sucesso!", "Sistema", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnCapturar.Enabled = false;
                    btnSalvar.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Foto não encontrada", "Sistema", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception erro)
            {
                throw erro;
            }
        }

        private void imgVideo_Click(object sender, EventArgs e)
        {

        }
    }
}
  • missed the Dysplasia when finished the action , Camera.Dispose()

  • Where do I put Dispose(); @Hudsonph? On the "Camera" object you do not have the Dispose function. I tried on "Captureinfo" but also could not.

  • Directx.Capture.Dispose ?

  • Does not have the "Dispose" function for Directx.Capture @Hudsonph

  • I got @Hudsonph. I put "Captureinfo.Disposecapture();". Thanks for your help!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.