Aforge.net Camera Capture & Save image to catalog

all. I was stuck here dealing with these errors for several days, but I still could not figure it out. My guess: I think my code has some problems, since I did not properly dispose of the object after using it (I am not very good at these concepts of resource release, streaming). I got this code, paying attention to what people did on YouTube, but despite the fact that I did the same, my code did not work well.

SITUATION: I have two windows with pictures, you can take a video with me on the left, take a picture to the right, if you press button 1, you will start the video, clone_button will copy the image, that is, take the picture, and save_image will save it in the path link, however, I get a general error that occurs in GDI + again and again while I try to save it. In addition, my debugger seemed crazy (i.e. vshost.exe failed to complete), as soon as I run this program, I need to restart my computer to run my code again, which is grim and frustrating.

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

Any AForge.net user can simply click the LINK button below and try it. Thank!

SAMPLE

+4
source share
1 answer

, , . , , . , , , , 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, , , , , .

, , . , lol. .

picurebox , , , , pbox2.image = pbox1.image, , , , pbox1 .

+4

Source: https://habr.com/ru/post/1548568/


All Articles