Matveev Pavel
Matveev Pavel

Reputation: 21

Taking webcam photos via C# + EmguCV isn't work

Taking webcam photos via C# + EmguCV isn't work. EmguCV version 3.1.0.1 (because of Visual Studio 2015, .NET Framework 4.5.2). OS Windows 10. My code (too much dispose() to be sure):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using ZXing;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Capture capture;
        Bitmap image;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (capture != null)
                {
                    capture.QueryFrame().Dispose();
                    capture.Dispose();
                }

                if (image != null)
                {
                    image.Dispose();
                }

                capture = new Capture();
                image = capture.QueryFrame().Bitmap;
                image.Save(Application.StartupPath + "\\img.jpg");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                if (capture != null)
                {
                    capture.QueryFrame().Dispose();
                    capture.Dispose();
                }

                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
    }
}

It's strange that this code works only one time and I get a photo. However, the app freezes on all subsequent attempts without any exception message. After restarting the computer, the application works correctly again only one time. With this behavior, it seems that some OS resource is not released after code execution.

Upvotes: 0

Views: 1352

Answers (1)

Matveev Pavel
Matveev Pavel

Reputation: 21

I modified my code with @sunside recommendations, and it works! Before asking on stackoverflow I tried many modifications, including without dispose, but they didn't work. I don't know where was a mistake. The working code is here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using ZXing;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Capture capture;
        Mat mat;
        Bitmap image;

        public Form1()
        {
            InitializeComponent();
            capture = new Capture();
            this.FormClosing += Form1_FormClosing;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (image != null) image.Dispose();
                if (mat != null) mat.Dispose();

                mat = capture.QueryFrame();
                image = mat.Bitmap;
                image.Save(Application.StartupPath + "\\img.jpg");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (image != null) image.Dispose();
                if (mat != null) mat.Dispose();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(capture != null) capture.Dispose();
        }
    }
}

Upvotes: 1

Related Questions