Question save photos taken by a webcam

Simo87

New member
Joined
Nov 26, 2016
Messages
1
Programming Experience
Beginner
Goodmorning everyone,
I am new to this forum and a beginner programmer;
I Created a program in C # with Visual Studio 2015 that, to the pressure of a button turns on the webcam , snaps a picture and displays it on a picture box; I adapted the Solution shown , from a Code that I Found on line but my ultimate good would another: I would like that , once you press the button, the photo was automatically saved in a specific folder ; The framework I used to perform operations on webcam is Aforge, but frankly I don't understand, from the documentation online of the library (aforge), how to perform the operation I have Just described; can you help me please?
I paste here my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video.VFW;
using AForge.Video;
//using System.IO;

namespace RiconoscimentoUtente2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool catturaFrame = false;
        VideoCaptureDevice webcam = null;
        Graphics superficieScatto = null;
        Rectangle rettangolo;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            superficieScatto = Scatto.CreateGraphics();
            rettangolo = Scatto.ClientRectangle;
            catturaFrame = true;
            if (webcam == null)
            {
                webcam = new VideoCaptureDevice("@device:pnp:\\\\?\\usb#vid_04f2&pid_b40e&mi_00#7&97d73eb&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global");

                webcam.NewFrame += new NewFrameEventHandler(fotogramma);
                webcam.Start();
            }

            using (System.IO.StreamWriter file =
                        new System.IO.StreamWriter(@"C:\Users\XXXXX pc\Desktop\RICONOSCIMENTO\SI.txt", true))
            {
                //file.WriteLine("+++++");
                MessageBox.Show("Ciao ZZZ!!!");
                file.WriteLine(DateTime.Now);
                //file.WriteLine();
                file.WriteLine("+++++");



            }

            Application.Exit();
            webcam.Stop();
            //MessageBox.Show("Ciao ZZZZ!!!");
            //this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            superficieScatto = Scatto.CreateGraphics();
            rettangolo = Scatto.ClientRectangle;
            catturaFrame = true;
            if (webcam == null)
            {
                webcam = new VideoCaptureDevice("@device:pnp:\\\\?\\usb#vid_04f2&pid_b40e&mi_00#7&97d73eb&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global");

                webcam.NewFrame += new NewFrameEventHandler(fotogramma);
                webcam.Start();

            }

            using (System.IO.StreamWriter file =
           new System.IO.StreamWriter(@"C:\Users\XXXXX pc\Desktop\RICONOSCIMENTO\NO.txt", true))
            {
                //file.WriteLine("+++++");
                MessageBox.Show("Salve Ospite!!!");
                file.WriteLine(DateTime.Now);
                //file.WriteLine();
                file.WriteLine("+++++");

            }

            Application.Exit();
            webcam.Stop();
        }

        void fotogramma(object sender, NewFrameEventArgs e)
        {
            if (catturaFrame)
            {
                superficieScatto.DrawImage(e.Frame, rettangolo);

                catturaFrame = false;
                //catturaFrame = true;
            }

        }
    }
    }


thank you so much
Hello
simone
 
Last edited by a moderator:
Bitmap.Save Method (System.Drawing)
e.Frame is a Bitmap, you can assign this to PictureBox.Image. CreateGraphics is anyway not the way to do it, if you want to paint the Bitmap to the control you should use Paint event handler for that.
Bitmap can be saved with Save method, use e.Frame.Save or store e.Frame in a variable and save later. This is also how you would use Paint event handler, you would take e.Frame from NewFrame event handler and assign it a variable, then refresh the PictureBox, in Paint event handler you would draw the Bitmap of that variable.
 
Back
Top Bottom