#dotnet – Display a video fileπŸŽ₯ in Winform using #OpenCV and #net5

Buy Me A Coffee

Hi !

Super short post today, however is the base of the next series of posts around OpenCV and .Net 5. I already wrote on how to work with a webcam, and now it’s time to share a simple piece of code to show

How to open and process a video frame by frame

I’ve downloaded a “traffic cam” video from Youtube, and this is the output in a Winform.

Super easy ! And as usual, the source code

using System;
using System.Threading;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Size = OpenCvSharp.Size;
namespace Demo13_WinFormVideoFromFile
{
public partial class Form1 : Form
{
private bool _run = false;
private VideoCapture _capture;
private Mat _image;
private Thread _cameraThread;
private string _videoFile = "4K camera example for Traffic Monitoring (Road).mp4";
private delegate void SafeCallDelegate(string text);
public Form1()
{
InitializeComponent();
Load += Form1_Load;
Closed += Form1_Closed;
}
private void Form1_Closed(object sender, EventArgs e)
{
_cameraThread.Interrupt();
_capture.Release();
}
private void btnStart_Click(object sender, EventArgs e)
{
_capture = new VideoCapture(_videoFile);
_run = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
_run = false;
}
private void Form1_Load(object sender, EventArgs e)
{
_image = new Mat();
_cameraThread = new Thread(new ThreadStart(CaptureCameraCallback));
_cameraThread.Start();
}
private void CaptureCameraCallback()
{
while (true)
{
if (!_run) continue;
var startTime = DateTime.Now;
_capture.Read(_image);
if (_image.Empty()) return;
var imageRes = new Mat();
Cv2.Resize(_image, imageRes, new Size(320, 240));
var bmpWebCam = BitmapConverter.ToBitmap(imageRes);
pictureBoxWebCam.Image = bmpWebCam;
}
}
}
}

And the source video

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


References

Advertisement

1 comment

  1. hello Sir

    I hope you are well. I am new to openCvSharp. I am facing a problem from past couple of days and the problem is i am able to capture a video from device but absolutely no video from file may it be a mp4 or a sdp.
    using
    OpenCvSharp 4.6 version
    OpenCvSharp.runtime.win 4.6

    I have installed latest OpenCV 4.7 on my System my OS is windows. I have added its path to bin folder path to my system and Enviromental variables.

    visual studio 2019. i am sharing my code,

    namespace cvsharpwebcam
    {
    public partial class Form1 : Form
    {
    VideoCapture capture;
    Mat frame;
    Bitmap image;
    public string _videoFile = “d:\video.mp4”;
    public Form1()
    {
    InitializeComponent();
    frame = new Mat();

            capture = new VideoCapture();
    
    
            //capture.Open("d://srtp.sdp", VideoCaptureAPIs.FFMPEG);
            capture.Open(_videoFile, VideoCaptureAPIs.ANY);
    
            if (!capture.IsOpened())
            {
                MessageBox.Show(" not capturing");
            }
        }
    
        public delegate void delegateShow(Bitmap b);
    
        private void SetPicture(Bitmap img)
        {
            //try
            //{
                if (pictureBox1.InvokeRequired)
                {
                    pictureBox1.Invoke(new MethodInvoker(
                    delegate ()
                    {
                        pictureBox1.Image = img;
                    }));
                }
                else
                {
                    pictureBox1.Image = img;
                }
            //}
            //catch(Exception ex)
            //{
            //    ;
            //}
        }
    
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            Thread t = new Thread(new ParameterizedThreadStart(ShowVideo));
            t.Start("");
    
    
    
        }
    
        private void ShowVideo(object ob)
        {
            while (true)
            {
                if (capture.IsOpened())
                {
                    capture.Read(frame);
                    image = BitmapConverter.ToBitmap(frame);
                    if (pictureBox1.Image != null)
                    {
                        pictureBox1.Image.Dispose();
                    }
    
                    SetPicture(image);
    
                    //pictureBox1.Image = image;
                }
            }
    
        }
    }
    

    }

    Above is my whole code which works fine for my videocapture from webcam but not from file.

    can you kindly guide me what I am doing wrong. Why videocapture is not working for files. I am clueless . please please any help will be greatly appreciated.
    i will be truly grateful.

    Best Regards,
    Sarah

    Like

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: