2013-04-04 150 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