0

I've done a ton of research and looked at a lot of questions here but can't seem to find anything to help me. I should preface I'm very new to C#, Windows Forms, and SO! I'm a 1st year CompSci student coming from C++ experimenting with my own projects for the summer. I'm trying to display a series of bitmaps from a .avi using the AForge.Video.FFMPEG video file reader.

It seems to be finding the file, getting its' data (console prints dimensions, framerate, and codec) and creating the picturebox, but the picturebox comes up blank/empty. I get the bitmap from the frames of a .avi:

From AForge example code here

Then I'm trying to display it with a picture box:

From MS example code here as well

And here's my code. Essentially a combination of the two:

    public class Simple : Form
{
    Bitmap videoFrame;

    public Simple()
    {
        try
        {
            // create instance of video reader
            VideoFileReader reader = new VideoFileReader();
            // open video file
            reader.Open(@"C:\Users\User\Desktop\ScanTest3.AVI");
            // check some of its attributes
            Console.WriteLine("width:  " + reader.Width);
            Console.WriteLine("height: " + reader.Height);
            Console.WriteLine("fps:    " + reader.FrameRate);
            Console.WriteLine("codec:  " + reader.CodecName);

            PictureBox pictureBox1 = new PictureBox();

            // read 100 video frames out of it
            for (int i = 0; i < 100; i++)
            {
                videoFrame = reader.ReadVideoFrame();

                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.ClientSize = new Size(videoFrame.Width, videoFrame.Height);
                pictureBox1.Image = videoFrame;

                // dispose the frame when it is no longer required
                videoFrame.Dispose();
            }

            reader.Close();
        }

        catch
        {
            Console.WriteLine("Nope");
        }

    }
}

class MApplication
{
    public static void Main()
    {
        Application.Run(new Simple());
    }
}

So that's it pretty much. Just a blank picture box coming up, when it should have the first frame of the video, even though no exception caught (though I'm pretty confident I'm using the try/catch very poorly), and the console printing the correct data for the file:

width:  720
height: 480
fps:    29
codec:  dvvideo
[swscaler @ 05E10060] Warning: data is not aligned! This can lead to a speedloss

Though if anyone could tell me what that warning means, that would be great as well, but I'm mainly just lost as to why there's no picture printing to the screen.

Thanks!

  • how about remove this `videoFrame.Dispose();` – Lei Yang Jul 31 '17 at 05:56
  • @LeiYang Problem still persists if `videoFrame.Dispose();` is removed. I believe it's necessary for the VideoFileReader to move to the next frame on next loop – smackledorf Jul 31 '17 at 05:58
  • what if not use for loop and only show one of the frames? – Lei Yang Jul 31 '17 at 05:59
  • @LeiYang Tried picturebox and videofilereader declaration outside of loop and problem persists – smackledorf Jul 31 '17 at 06:00
  • why creating `PictureBox` at runtime? – Lei Yang Jul 31 '17 at 06:01
  • @LeiYang wouldn't normally but was having this problem in my actual project, so made a new project to try it here and problem persisted. Unless you mean why inside the form in which case I moved it to where I declare `Bitmap videoFrame` and problem persists still – smackledorf Jul 31 '17 at 06:03

1 Answers1

0

Found This post, where he uses timers to update, and it works perfectly. I'm guessing pictureBox only really works in a function maybe, not sure. Still learning. Thanks!