2016-10-22 78 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(); 
     } 
    }