PBKK - Tugas Pertemuan 3

 Nama        : Al-Ferro Yudisthira Putra

NRP         : 5025211176

Kelas        : PBKK A

Berikut merupakan salah satu tugas dari mata kuliah PBKK yang saya kerjakan. Tugas tersebut berupa membuat project windows application menggunakan .NET. Pada kesempatan kali ini saya membuat sebuah form yang mampu mengcapture gambar serta video melalui webcam yang tersedia. Form ini memiliki fungsi untuk memilih webcam yang terhubung, memulai camera webcam, mengambil gambar, serta menyimpan gambar dengan format png atau jpg. Berikut merupakan tampilannya,



Kemudian berikut merupakan code menggunakan bahasa C# untuk pengimplementasian fungsi-fungsi yang tersedia.

using AForge;

using AForge.Video;

using AForge.Video.DirectShow;

using System.Drawing;

using System.Drawing.Imaging;


namespace VideoCapture

{

    public partial class Form1 : Form

    {

        private FilterInfoCollection captureDevice;

        private VideoCaptureDevice videoSource;

        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo deviceList in captureDevice)

            {

                comboBoxWebCamList.Items.Add(deviceList.Name);

            }

            comboBoxWebCamList.SelectedIndex = 0;

            videoSource = new VideoCaptureDevice();

        }


        private void start_Click(object sender, EventArgs e)

        {

            if (videoSource.IsRunning)

            {

                videoSource.SignalToStop();

                videoSource.WaitForStop();

                pictureBox1.Image = null;

                pictureBox1.Invalidate();


            }

            videoSource = new VideoCaptureDevice(captureDevice[comboBoxWebCamList.SelectedIndex].MonikerString);

            videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);

            videoSource.Start();


        }

        private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)

        {


            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();

        }


        private void capture_Click(object sender, EventArgs e)

        {

            pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();

        }


        private void SaveImage_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Title = "Save Image As";

            saveFileDialog.Filter = "Image files(*.jpg, *.png) | *.jpg, *.png";

            ImageFormat imageFormat = ImageFormat.Png;


            if (saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                string ext = System.IO.Path.GetExtension(saveFileDialog.FileName);

                switch (ext)

                {

                    case ".jpg":

                        imageFormat = ImageFormat.Jpeg;

                        break;

                    case ".png":

                        imageFormat = ImageFormat.Png;

                        break;


                }

                pictureBox2.Image.Save(saveFileDialog.FileName, imageFormat);

            }

        }


        private void Exit_Click(object sender, EventArgs e)

        {

      

            if (videoSource.IsRunning)

            {

                videoSource.SignalToStop();

                videoSource.WaitForStop();

                pictureBox1.Image = null;

                pictureBox2.Image= null;

                pictureBox1.Invalidate();

                pictureBox2.Invalidate();   

            }

            Application.Exit(null);


        }

    }

}


Kemudian berikut merupakan code menggunakan bahasa C# untuk pengimplementasian fungsi-fungsi yang tersedia.


Berikut merupakan link dari repository yang berada di GitHub

Comments

Popular posts from this blog

PBKK - Final Project Pemrograman Berbasis Kerangka Kerja

PBKK - Tugas Pertemuan 6