2014-07-16 68 views
4

所有人。我一直被困在這裏處理這些錯誤的日子,但我仍然無法弄清楚。 我的猜想:我認爲我的代碼有一些問題,因爲我在使用它之後沒有正確地處理對象(我不太瞭解釋放資源,線程的這些概念)。 我通過參考人們在YouTube上做過的事情得到了這些代碼,但儘管我做了完全相同的事情,但我的代碼並沒有很好地工作。Aforge.net相機捕捉並保存圖像到目錄

狀態: 我有兩個圖片框,左側可以拍攝我的視頻,右側拍攝快照,如果按下按鈕1,您將開始播放視頻,clone_button將複製圖像,即拍攝快照,save_image應該將它保存到路徑引用中,但是,當我試圖保存它時,我一次又一次地在GDI +中發生了一個通用錯誤。另外,一旦我運行這個程序時,我的調試器似乎變得瘋狂(即無法終止vshost.exe),我必須重新啓動計算機才能讓代碼再次運行,這是令人沮喪和沮喪的。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Drawing.Imaging; 
//AForge.Video dll 
using AForge.Video; 
using AForge.Video.DirectShow; 
using AForge.Imaging; 
using AForge.Imaging.Filters; 
using AForge; 


namespace WebCameraCapture 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private FilterInfoCollection CaptureDevice; // list of webcam 
     private VideoCaptureDevice FinalFrame; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);//constructor 
      foreach (FilterInfo Device in CaptureDevice) 
      { 
       comboBox1.Items.Add(Device.Name); 
      } 

      comboBox1.SelectedIndex = 0; // default 
      FinalFrame = new VideoCaptureDevice(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);// specified web cam and its filter moniker string 
      FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);// click button event is fired, 
      FinalFrame.Start(); 
     } 

     void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) // must be void so that it can be accessed everywhere. 
    // New Frame Event Args is an constructor of a class 
     {  
      pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();// clone the bitmap 
     } 

     private void From1_CLosing(object sender, EventArgs e) 
     { 
      if (FinalFrame.IsRunning==true) FinalFrame.Stop(); 
     } 

     private void save_Click(object sender, EventArgs e) 
     { 
      if (pictureBox2.Image != null) 
      { 
       Bitmap varBmp = new Bitmap(pictureBox2.Image); 
       Bitmap newBitmap = new Bitmap(varBmp); 
       varBmp.Dispose(); 
       varBmp = null; 
       varBmp.Save(@"C:\a.png", ImageFormat.Png); 
      } 
      else 
      { MessageBox.Show("null exception"); } 
     } 

     private void clone_Click(object sender, EventArgs e) 
     { 
      pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); 
     } 
    } 
} 

任何AForge.net用戶都可以按下下面的鏈接並試用它。謝謝!

SAMPLE

回答

4

在看看你的代碼,在我之後,似乎您要處理圖像的保存它的權利之前。這意味着你的程序不能保存圖像,因爲它不再存在。實際上,它顯示你已經基本上刪除了捕獲的圖像兩次,一旦處置,第二次當你將它設置爲空。

所以,如果你在保存後移動兩個代碼段,它應該工作。在沒有使用對話框來更改文件名稱的情況下,您一定會收到錯誤信息,除非您在每次創建文件後都刪除該文件。

private void save_Click(object sender, EventArgs e) 
    { 
     if (pictureBox2.Image != null) 
     { 
      //Save First 
      Bitmap varBmp = new Bitmap(pictureBox2.Image); 
      Bitmap newBitmap = new Bitmap(varBmp); 
      varBmp.Save(@"C:\a.png", ImageFormat.Png); 
      //Now Dispose to free the memory 
      varBmp.Dispose(); 
      varBmp = null; 
     } 
     else 
     { MessageBox.Show("null exception"); } 
    } 

如果你打開任務管理器,你可以看到你的程序吸收了多少內存。 完成使用後丟棄內存,將其返回系統。 您的FinalFrame_NewFrame線程內沒有配置,因此當相機正在讀取圖像時,您應該看到內存使用率繼續攀升,直到您停止該程序。

我已經添加配置到我的線程,把內存使用情況控制在,但現在我調試我的圖像保存。因爲我正在處理,我無法保存圖像哈哈。我的程序最終嘗試保存一個空映像文件並引發相應的錯誤。

我正在使用第二個picurebox,但使用例如pbox2.image = pbox1.image,不復制數據,它複製與圖像數據的內存位置,所以當我處理pbox1釋放內存,圖像數據隨內存位置消失。

+0

超級,工作真棒..... – Sunil

相關問題