Picture box not displaying Emgu.cv.mat.toBitmap image

Chidera Nwosu 0 Reputation points
2023-01-31T12:26:49.3166667+00:00

Was following a tutorial on YouTube and tried to get my program to display the live video feed from my webcam into the forms picture box but for some reason the webcam comes on but the picture box isn't displaying anything I have tried checking if the Mat class was returning null.

but it isn't this is my code

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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;

namespace FaceRecognitionAttandanceSystem
{
    public partial class StudentAdd : Form
    {
        public string fileName { get; set; }
        VideoCapture capture;
        
        public StudentAdd()
        {
            InitializeComponent();
        }
        
        //Open Folder
        private void metroButton3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false };
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //open file explorer
                    fileName = openFileDialog.FileName;
                    //pick image from file
                    Image img = Image.FromFile(fileName);
                    //Rotates Image by 90degrees
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    pictureBox2.Image = img;
                }
            }

        }

        //Capture Image
        private void metroButton4_Click(object sender, EventArgs e)
        {
            //If capture is empty start new video capture
            if(capture == null)
            {
                capture = new Emgu.CV.VideoCapture(0);           
            }
            capture.ImageGrabbed += Capture_ImageGrabbed;
            capture.Start();
        }

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {
                Mat mat = new Mat();
                if(mat == null)
                {
                    Console.WriteLine("Here you Go");
                }
                capture.Retrieve(mat);
                pictureBox1.Image = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
            }
            catch(Exception)
            {

            }
        }

        private void metroButton5_Click(object sender, EventArgs e)
        {
            
        }
    }
}
Developer technologies | Windows Forms
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Omane Stephen 0 Reputation points
    2023-04-18T13:54:23.7633333+00:00
    Header 1 Header 2
    Cell 1 Cell 2
    Cell 3 Cell 4
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.