首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Aforge保存图像

使用Aforge保存图像
EN

Stack Overflow用户
提问于 2013-04-04 14:16:12
回答 1查看 12.1K关注 0票数 0

我写了一个访问摄像头的代码,点击后将图片保存到一个文件夹中。

代码语言:javascript
复制
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();)显示了一个空引用异常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-04 15:06:55

就我在您的代码中所看到的,新图像帧被复制到您的成员变量bitmap。静态成员_latestFrame似乎从未被赋值过。

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

代码语言:javascript
复制
Bitmap current = (Bitmap)bitmap.Clone();

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

我还认为您过度使用了button2_Click方法中的filepath设置。首先,只需验证图片是否可以正确保存到active directory,方法是将您的button2_Click方法更改为:

代码语言:javascript
复制
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按钮时,都会将新图像写入“当前目录”。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15803811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档