C# Aforge.net摄像头捕获和;将图像保存到目录

C# Aforge.net摄像头捕获和;将图像保存到目录,c#,debugging,dispose,aforge,C#,Debugging,Dispose,Aforge,各位。我已经被困在这里处理这个bug好几天了,但我仍然无法理解它。 我的猜测是:我认为我的代码有一些问题,因为我在使用对象后没有正确地处理它(我不太熟悉释放资源、线程等概念)。 我通过参考人们在youtube上所做的事情得到了这些代码,但尽管我做了完全相同的事情,我的代码并没有很好地工作 情况: 我有两个图片框,左一个可以拍摄我的视频,右一个可以拍摄快照,如果您按下按钮1,您将启动视频,克隆按钮将复制图像,即拍摄快照,保存图像应将其保存到路径引用,但是,我在尝试保存时在GDI+中反复出现一般错误

各位。我已经被困在这里处理这个bug好几天了,但我仍然无法理解它。 我的猜测是:我认为我的代码有一些问题,因为我在使用对象后没有正确地处理它(我不太熟悉释放资源、线程等概念)。 我通过参考人们在youtube上所做的事情得到了这些代码,但尽管我做了完全相同的事情,我的代码并没有很好地工作

情况: 我有两个图片框,左一个可以拍摄我的视频,右一个可以拍摄快照,如果您按下按钮1,您将启动视频,克隆按钮将复制图像,即拍摄快照,保存图像应将其保存到路径引用,但是,我在尝试保存时在GDI+中反复出现一般错误。此外,我的调试器似乎变得疯狂(即未能终止vshost.exe)。一旦我运行此程序,我就必须重新启动计算机以使代码再次运行,这是令人沮丧和沮丧的

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 System.IO;
using System.Drawing.Imaging;
//AForge.Video dll
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;


namespace WebCameraCapture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private FilterInfoCollection CaptureDevice; // list of webcam
        private VideoCaptureDevice FinalFrame;

        private void Form1_Load(object sender, EventArgs e)
       {
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);//constructor
            foreach (FilterInfo Device in CaptureDevice)
            {
                comboBox1.Items.Add(Device.Name);
            }

            comboBox1.SelectedIndex = 0; // default
            FinalFrame = new VideoCaptureDevice();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);// specified web cam and its filter moniker string
            FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);// click button event is fired, 
            FinalFrame.Start();
        }

        void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) // must be void so that it can be accessed everywhere.
    // New Frame Event Args is an constructor of a class
        {     
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();// clone the bitmap
        }

        private void From1_CLosing(object sender, EventArgs e)
        {
            if (FinalFrame.IsRunning==true) FinalFrame.Stop();
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image != null)
            {
                Bitmap varBmp = new Bitmap(pictureBox2.Image);
                Bitmap newBitmap = new Bitmap(varBmp);
                varBmp.Dispose();
                varBmp = null;
                varBmp.Save(@"C:\a.png", ImageFormat.Png);
            }
            else
            { MessageBox.Show("null exception"); }
        }

        private void clone_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
        }
   }
}
任何一个Forge.net用户都可以点击下面的链接并尝试一下。谢谢


查看代码后,我觉得您在保存图像之前正在处理图像。这意味着你的程序无法保存图像,因为它已经不存在了。实际上,它显示您实际上已经删除了捕获的图像两次,一次是在dispose上,第二次是在将其设置为null时

因此,如果在保存后移动这两个代码段,它应该可以工作。如果不使用对话框更改文件名,您肯定会收到错误,除非您在每次创建文件后删除该文件

private void save_Click(object sender, EventArgs e)
    {
        if (pictureBox2.Image != null)
        {
            //Save First
            Bitmap varBmp = new Bitmap(pictureBox2.Image);
            Bitmap newBitmap = new Bitmap(varBmp);
            varBmp.Save(@"C:\a.png", ImageFormat.Png);
            //Now Dispose to free the memory
            varBmp.Dispose();
            varBmp = null;
        }
        else
        { MessageBox.Show("null exception"); }
    }
如果打开任务管理器,可以查看程序占用的内存量。 在使用完内存后,将其释放,并将其返回给系统。 在FinalFrame_NewFrame线程中没有dispose,因此当相机读取图像时, 在停止程序之前,您应该会看到内存使用率继续攀升

我已经在线程中添加了dispose,控制了内存使用,但现在我正在调试映像保存。因为我正在处理,所以无法保存图像lol。我的程序最终尝试保存空图像文件并抛出相应的错误

我和您一样使用第二个picurebox,但使用例如pbox2.image=pbox1.image,不会复制数据,它会用图像数据复制内存位置,因此当我释放pbox1以释放内存时,图像数据会随内存位置一起消失