Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Emgu CV to play a video inside a windows forms project through the use of a picture box control. I pause the thread for exactly 33 seconds before updating the current image on the picture box to create the impression of a video. The video plays for the first few seconds before the program crashes with this error message on the debug window
program has exited with code (0x3)

What I have tried:

Here is how I am trying to achieve this. I have an open file dialog that let's a user select a video file. After user selects a video file then I load it into an emgu cv video capture object. I then use a while loop to check if the video frames can be grabbed then I change the picture on the picture box and then I use Task.Delay to enforce the frame rate behavior. The video plays in the picture box for the first few seconds and then the program quits with the error message shown above. Inspect my code and propose possible fixes, Thanks.

C#
public partial class Form1 : Form{
  //declare the video capture object
  private VideoCapture video;
  //declare the frame container object
  private Mat mat = new Mat();
  
  //this method let's a user choose a video for playing
  private void loadVideo(object sender, EventArgs e){
    //declare the open file dialog 
   using(var openfiledialog = new OpenFileDialog()){
       //set video file filters
       openfiledialog.Filter = "Video Files|*.mp4;*.avi";
       if(openfiledialog.ShowDialog() == DialogResult.OK){
         //get the extension and make sure its a video file
         var ext = Path.GetExtension(openfiledialog.FileName);
         if(ext.Contains(".mp4") || ext.Contains(".avi")) 
           {
            //load the video into a emgu cv capture object
            video = new VideoCapture(openfilediaog.FileName);
             //call method to play video
             PlayVideo();
            }
       }
    }
    }
   //method to play the video
   private void async PlayVideo(){
   while(video.Grab()){
      //read a frame and display 
      using(var frame = new Mat()){
       CvInvoke.Resize(frame, frame,pictureBox1.Size);
      //convert the Mat to bitmap
      var bmp = ToBitmap(frame);
     //delay the thread for 33 seconds for video 
     Task.Delay(33);
        }
      }
   }
}

How do I revise my code to get rid of this error?
Posted
Updated 13-Aug-23 22:37pm

1 solution

//delay the thread for 33 seconds for video
Task.Delay(33);
33 isn't 33 seconds, it's 33 milliseconds:
Task.Delay Method (System.Threading.Tasks) | Microsoft Learn[^]
The chances are that you are running out of system resources since you are merrily creating bitmaps and not Disposing them.

Why are you doing it like this anyway, when there are controls you can drop into your form that will play it for you? Have a look here: Embedding the Windows Media Player Control in a C# Solution - Win32 apps | Microsoft Learn[^]
 
Share this answer
 
v2
Comments
Tim the Gamer 14-Aug-23 4:45am    
Thanks for your comment, yes I believe am not disposing of the Mat efficiently to minimize memory usage as much as possible. I just want to learn how to build a native video player from scratch where I read all the images in the video and use some mechanism to time the presentation of the frames and create impression of a video to the user. How do I fix this? Can you reproduce the issue on your end and recommend a solution based on the code I provided,Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900