2

I recently wanted to try out AForge.NET as I found it to be quite simple, so I decided to make some simple video playback using Video.FFMPEG namespace from it, so I could put each frame directly on a pictureBox. That alone works well, but I wanted to dispose every image after not being important because it took about 1.5GB memory for no apparent reason. That's where my problem started. For some reason, it sometimes will just throw this exception (usually when resizing the window). I'm not really sure what it could be caused by. Maybe it's really a stupid mistake. My guess is that it could be caused by timer, but I could have done a completely different mistake and just cannot see it. This is the exception I keep getting:

    ************** Exception Text **************
System.ArgumentException: Parameter is not valid.
   at System.Drawing.Image.get_Width()
   at System.Drawing.Image.get_Size()
   at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This is the code (I do realize that public variables are not good, I'm just testing of course):

public long i = 0;
public Bitmap img;
public VideoFileReader reader;
public System.Timers.Timer aTimer;

public void render(object source, ElapsedEventArgs e)
{
    if (img != null) img.Dispose();
    if (i < reader.FrameCount)
    {
        img = reader.ReadVideoFrame();
        pictureBox1.Image = img;
    }
    i++;
}

private void button1_Click(object sender, EventArgs e)
{
    reader = new VideoFileReader();
    aTimer = new System.Timers.Timer();
    reader.Open("d:\\result.avi");
    aTimer.Elapsed += new ElapsedEventHandler(render);
    aTimer.Interval = reader.FrameRate;
    aTimer.Enabled = true;
}
Dulcia
  • 73
  • 1
  • 1
  • 7

1 Answers1

0

I guess I missed something when it comes to timers, they don't seem to work the best for this case. For people who would like to use AForge.NET for playback, this might be a solution. I put off the timers and used backgroundWorker with Stopwatch instead, no issues occuring so far.

    public Image img;
    public VideoFileReader reader;

    private void button1_Click(object sender, EventArgs e)
    {
        reader = new VideoFileReader();
        reader.Open("d:\\result.avi");
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Stopwatch watch = new Stopwatch();
        for (i=0;i<reader.FrameCount;i++)
        {
            img = pictureBox1.Image;
            pictureBox1.Image = reader.ReadVideoFrame();
            if (img != null) img.Dispose();
            watch.Start();
            while (watch.ElapsedMilliseconds < reader.FrameRate);
            watch.Stop();
            watch.Reset();
        }
    }
Dulcia
  • 73
  • 1
  • 1
  • 7