Aforge.net相机捕获并将图像保存到目录

el *_*roo 6 c# debugging dispose aforge

大家.我已经被困在这里处理这些错误了好几天,但我仍然无法弄明白.我的猜测:我认为我的代码存在一些问题,因为我在使用它之后没有正确处理对象,(我不太熟悉释放资源,线程的这些概念).我通过参考人们在youtube上做了什么来获得这些代码,但是尽管我做了完全相同的事情,但我的代码并没有很好地解决.

情况:我有两个图片框,左边一个可以拍摄我的视频,右边一个拍摄快照,如果你按下button1,你将启动视频,clone_button将复制一个图像,即拍摄快照,save_image应该将它保存到但是,当我试图保存它时,我在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();
        }
   }
}
Run Code Online (Sandbox Code Playgroud)

任何AForge.net用户都可以按下面的LINK并尝试一下.谢谢!

样品

小智 4

查看您的代码后,对我来说,您似乎在保存图像之前就将其处理掉。这意味着您的程序无法保存图像,因为它不再存在。实际上,它显示您基本上已经删除了捕获的图像两次,一次是在处理时,第二次是将其设置为空时。

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

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"); }
    }
Run Code Online (Sandbox Code Playgroud)

如果打开任务管理器,您可以查看程序占用了多少内存。使用完内存后将其处理掉,将其返还给系统。您的 FinalFrame_NewFrame 线程内没有处置,因此当相机读取图像时,您应该看到内存使用量继续攀升,直到您停止程序。

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

我和你一样使用第二个picurebox,但是使用例如 pbox2.image = pbox1.image,不会复制数据,它会复制带有图像数据的内存位置,所以当我处置 pbox1 以释放内存时,图像数据随内存位置消失。