0

ウェブカメラにアクセスし、クリックして画像をフォルダーに保存するためのコードを作成しました。

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 で、画像を保存するためのコードを書きました。プログラムをビルドすると、指定された行に対して null 参照例外が表示されます( Bitmap current = (Bitmap)_latestFrame.Clone();)

4

1 に答える 1

1

あなたのコードを見る限り、新しい画像フレームがメンバー変数にコピーされていますbitmap。静的メンバー_latestFrameは割り当てられていないようです。

したがって、button2_Clickメソッドの最初の行を次のように変更します。

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

ボタンをクリックしたときに Web カメラから少なくとも 1 つのフレームを受信して​​いれば、フレームは適切に保存されます。

また、メソッドのfilepath設定をやりすぎていると思います。button2_Clickまず、button2_Click方法を次のように変更して、画像が Active Directory に適切に保存されることを確認します。

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

これにより、[キャプチャ] ボタンをクリックするたびに、新しいイメージが「現在のディレクトリ」に書き込まれます。

上記の変更を加えてコードをテストしましたが、問題なく動作します。

于 2013-04-04T07:06:55.013 に答える