C# Free Source Code Capture webcam images and save them to your computer


C# Free Source Code: Capture webcam images and save them to your computer

Note: You need to download and install the WebCam_Capture.dll file.


Visual Studio 2019 ถ่ายรูปจากกล้อง Webcam หรือกล้องมือถือ แล้วเซฟไฟล์ลงบนคอม.


Class: WebCam.cs


using WebCam_Capture;

namespace WinFormCharpWebCam

{

    class WebCam

    {

        private WebCamCapture webcam;

        private System.Windows.Forms.PictureBox _FrameImage;

        private int FrameNumber = 30;

        public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)

        {

            webcam = new WebCamCapture();

            webcam.FrameNumber = ((ulong)(0ul));

            webcam.TimeToCapture_milliseconds = FrameNumber;

            webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);

            _FrameImage = ImageControl;

        }

        void webcam_ImageCaptured(object source, WebcamEventArgs e)

        {

            _FrameImage.Image = e.WebCamImage;

        }

        public void Start()

        {

            webcam.TimeToCapture_milliseconds = FrameNumber;

            webcam.Start(0);

        }

        public void Stop()

        {

            webcam.Stop();

        }

        public void Continue()

        {

            // change the capture time frame

            webcam.TimeToCapture_milliseconds = FrameNumber;

            // resume the video capture from the stop

            webcam.Start(this.webcam.FrameNumber);

        }

        public void ResolutionSetting()

        {

            webcam.Config();

        }

        public void AdvanceSetting()

        {

            webcam.Config2();

        }

    }

}


Class: Helper.cs


using System.IO;
using System.Windows.Forms;
namespace WinFormCharpWebCam
{
    class Helper
    {
        public static void SaveImageCapture(System.Drawing.Image image)
        {

            SaveFileDialog s = new SaveFileDialog();
            s.FileName = "Image";// Default file name
            s.DefaultExt = ".Jpg";// Default file extension
            s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

            // Show save file dialog box
            // Process save file dialog box results
            if (s.ShowDialog()==DialogResult.OK)
            {
                // Save Image
                string filename = s.FileName;
                FileStream fstream = new FileStream(filename, FileMode.Create);
                image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
                fstream.Close();
            }
        }
    }
}


Form1.cs

using System;
using System.Windows.Forms;
using WinFormCharpWebCam;

namespace CSWebCamWinForms1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WebCam webcam;
        private void Form1_Load(object sender, EventArgs e)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgVideo);
        }

        private void bntStart_Click(object sender, EventArgs e)
        {
            webcam.Start();
        }

        private void bntStop_Click(object sender, EventArgs e)
        {
            webcam.Stop();
            imgVideo.Image = null;
        }

        private void bntContinue_Click(object sender, EventArgs e)
        {
            webcam.Continue();
        }

        private void bntCapture_Click(object sender, EventArgs e)
        {
            if (imgVideo.Image != null)
            {
                imgCapture.Image = imgVideo.Image;
            }
            else
            {
                imgCapture.Image = null;
            }
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            if (imgCapture.Image != null)
            {
                Helper.SaveImageCapture(imgCapture.Image);
            }
        }
    }
}


Comments