2013-04-04 82 views
0

我編寫了一個訪問網絡攝像頭的代碼並點擊,將圖片保存到文件夾中。使用Aforge保存圖像

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 AForge.Imaging; 
using AForge.Imaging.Filters; 
using AForge.Video; 
using AForge.Video.DirectShow; 

namespace cam 
{ 
public partial class Form1 : Form 
{ 
    public static Bitmap _latestFrame; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private FilterInfoCollection webcam; 
    private VideoCaptureDevice cam; 
    Bitmap bitmap; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
     foreach (FilterInfo VideoCaptureDevice in webcam) 
     { 
      comboBox1.Items.Add(VideoCaptureDevice.Name); 

     } 
     comboBox1.SelectedIndex = 0; 
     } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString); 
     cam.NewFrame += new NewFrameEventHandler(cam_NewFrame); 
     cam.Start(); 

    } 
    void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 
     bitmap = (Bitmap)eventArgs.Frame.Clone(); 

     pictureBox1.Image = bitmap; 
    } 
    private void pictureBox1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Image = bitmap; 
    } 
    private void button3_Click(object sender, EventArgs e) 
    { 
     if (cam.IsRunning) 
     { 
      cam.Stop(); 
     } 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     Bitmap current = (Bitmap)_latestFrame.Clone(); 
     string ActiveDir = AppDomain.CurrentDomain.BaseDirectory; 
     string filepath = System.IO.Path.Combine(ActiveDir, @"D://picture/"); 
     if (!System.IO.Directory.Exists(filepath)) 
     { 
      System.IO.DirectoryInfo OutputDir = System.IO.Directory.CreateDirectory(filepath); 
      string fileName = System.IO.Path.Combine(filepath, @"name.bmp"); 
      if (!System.IO.File.Exists(fileName)) 
      { 
       current.Save(fileName); 
      } 
     } 
     current.Dispose(); 
    } 

    } 

    } 

在我寫的代碼保存圖片,建設程序的按鈕2,顯示了一個空引用異常爲給定線(Bitmap current = (Bitmap)_latestFrame.Clone();

+0

在上面的代碼中,'_latestFrame'似乎沒有被賦值? – 2013-04-04 06:23:24

+0

是否應該爲空? – Aswathy 2013-04-04 06:28:28

+0

在克隆它之前,必須將_latestFrame設置爲非空值。就我的代碼而言,'_latestFrame'永遠不會被設置,因此當你調用'_latestFrame.Clone()'時,一個空引用異常顯然會被拋出。 – 2013-04-04 06:34:51

回答

1

至於我可以看到你代碼,新的圖像幀被複制到您的成員變量bitmap。靜態成員_latestFrame似乎從未被分配。

因此,在你的button2_Click方法,改變第一行:

Bitmap current = (Bitmap)bitmap.Clone(); 

現在,只要你收到至少一個幀從網絡攝像頭,當您單擊該按鈕,該框架應妥善保存。

我還認爲您正在過度使用button2_Click方法中的filepath設置。首先,簡單地確認畫面可以正確保存到Active Directory通過更改button2_Click方法是:

private void button2_Click(object sender, EventArgs e) 
{ 
    Bitmap current = (Bitmap)bitmap.Clone(); 
    string filepath = Environment.CurrentDirectory; 
    string fileName = System.IO.Path.Combine(filepath, @"name.bmp"); 
    current.Save(fileName); 
    current.Dispose(); 
} 

這將確保一個新的圖像將被寫入「當前目錄」每次你點擊Capture按鈕。

我已經測試了上述更改的代碼,它的工作原理完美無瑕。

+0

我做了更改bt什麼都沒有保存在圖片文件夾:-( – Aswathy 2013-04-04 07:34:05

+0

請參閱更新的答案的其他信息 – 2013-04-04 08:04:08

+0

嗨,先生感謝您的支持..我做了更改,但當前目錄可以是任何東西,我們可以指定它到一個特定的文件夾。提前感謝 – Aswathy 2013-04-04 08:42:19