2016-10-22 83 views
0

我最近想尝试AForge.NET,因为我发现它很简单,所以我决定使用Video.FFMPEG命名空间来做一些简单的视频回放,所以我可以将每个帧直接放在一个pictureBox上。这一方面效果很好,但我想在不重要的情况下处理每个图像,因为无需明显的原因,大约需要1.5GB的内存。这就是我的问题开始的地方。出于某种原因,它有时会抛出此异常(通常在调整窗口大小时)。我不确定可能是由什么造成的。也许这真的是一个愚蠢的错误。我的猜测是,这可能是由计时器造成的,但我可以完成一个完全不同的错误,但看不到它。这是我不断收到异常:pictureBox图像配置异常

************** 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) 

这是代码(我也知道公共变量都不好,我只是在测试过程中的):

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; 
} 

回答

0

我想我错过了当涉及到定时器的时候,他们似乎并没有为这种情况最好地工作。对于想要使用AForge.NET进行播放的用户,这可能是一个解决方案。我推迟了定时器,并使用了带有秒表的backgroundWorker,而没有发生问题。

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(); 
     } 
    }