【问题标题】:Saving images using Aforge使用 Aforge 保存图像
【发布时间】:2013-04-04 06:16:12
【问题描述】:

我编写了一段代码,用于访问网络摄像头并在点击时将图片保存到文件夹中。

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

    }

    }

在button2中我已经编写了保存图片的代码,在构建程序时,给定行显示了一个空引用异常(Bitmap current = (Bitmap)_latestFrame.Clone();

【问题讨论】:

  • 在上面的代码中,_latestFrame 似乎从未被赋予任何值?
  • 是否应该设为null?
  • 在克隆它之前,_latestFrame 必须设置为非空值。据我在您的代码中看到的,_latestFrame 从未设置,因此当您调用_latestFrame.Clone() 时,显然会抛出空引用异常。
  • 我是 csharp 的新手。如果我没记错的话,我刚刚初始化了网络摄像头。现在我需要在保存之前捕获它。对吗?

标签: c# aforge


【解决方案1】:

据我在您的代码中看到的,新的图像框架被复制到您的成员变量bitmap。静态成员 _latestFrame 似乎从未被分配。

因此,在您的button2_Click 方法中,将第一行更改为:

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

现在,如果您在单击按钮时至少从网络摄像头接收到一帧,则该帧应该已正确保存。

我还认为您在 button2_Click 方法中过度使用了 filepath 设置。首先,只需将您的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();
}

这将确保每次单击捕获按钮时都会将新图像写入“当前目录”。

我已经使用上述更改测试了您的代码,它可以完美运行。

【讨论】:

  • 我做了更改,但图片文件夹中没有保存任何内容:-(
  • 您好,先生,感谢您的支持..我做了更改,但当前目录可以是任何内容,我们可以将其指定到特定文件夹。提前致谢
  • 是的,我只是建议CurrentDirectory 作为第一个测试。只要路径名有效并且您对该位置具有写入权限,您就可以指定任何文件夹。
  • 先生,如果您不介意,我可以再问一个问题。正如你所说的,每次我点击它时都会覆盖以前的图像。以任何方式不要覆盖它而不是保存为另一个图像
  • 先生,我使用了增量程序并且正在按我的需要工作:-) 非常感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2013-11-08
相关资源
最近更新 更多