C# : salvare foto da webcam in cartella specifica

di il
4 risposte

C# : salvare foto da webcam in cartella specifica

Buongiorno a tutti,
sono nuovo di questo forum e un programmatore principiante;
ho creato un programma in C# con Visual Studio 2015 che alla pressione di un bottone accende la web cam e scatta una foto e la mostra su un picture box; la soluzione appena mostrata l'ho riadattata da un codice che ho trovato on line ma il mio fine ultimo sarebbe un altro : vorrei che, una volta premuto il bottone, la foto scattata venisse salvata automaticamente in una cartella specifica; il framework che ho usato per eseguire le operazioni sulla webcam è Aforge , ma francamente non riesco a capire, dalla documentazione on line della libreria (aforge) se e come eseguire le operazioni che ho appena illustrato; sareste così gentili da aiutarmi?
vi incollo il codice che ho scritto fin'ora
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;
            }

        }
    }
    }


Grazie mille
Cari saluti
Simone

4 Risposte

  • Re: C# : salvare foto da webcam in cartella specifica

    Se il Frame è una Bitmap, salva la bitmap su file. Documentati sulla classe Bitmap .NET e come salvarla su disco.
  • Re: C# : salvare foto da webcam in cartella specifica

    Grazie mille per la risposta; ma cosa intendi con "Se il Frame è una Bitmap"?
    Scusa se la domanda è sciocca
    Grazie ancora
  • Re: C# : salvare foto da webcam in cartella specifica

    Se l'oggetto e.Frame che utilizzi nell'evento è una Bitmap (come appare dalla documentazione online) ...
  • Re: C# : salvare foto da webcam in cartella specifica

    Grazie mille per l'aiuto: ho fatto così
    void fotogramma(object sender, NewFrameEventArgs e)
    {
    
    if (catturaFrame)
    {
    
    superficieScatto.DrawImage(e.Frame, rettangolo);
    Image foto = e.Frame;
    foto.Save("foto");
    catturaFrame = false;
    
    }
    
    }
    ed effettivamente adesso mi salva la foto scattata dandole il nome "foto" all'interno del mio progetto nella cartella >bin ;
    ogni volta che lancio il programma ovviamente mi sovrascrive la nuova foto scattata alla precedente;
    se volessi invece che la nuova foto scattata non sovrascrivesse la vecchia ma si aggiungesse all'elenco mantenendo tutte le foto scattate come potrei fare?
    Inoltre se volessi cambiare la cartella di salvataggio come potrei agire?
    Grazie mille
    saluti
    Simone
Devi accedere o registrarti per scrivere nel forum
4 risposte