Tugas PBKK A Pertemuan 3

 Nama     : Meyroja Jovancha Firoos

NRP        : 5025211204

Kelas       : PBKK A


Halo semuanya! Kali ini, saya ingin membagikan pengalaman dalam membuat Project Cam Capture App ( Pengambilan Gambar ) untuk Desktop. Project ini dirancang menggunakan .NET Framework dan bahasa C# serta dibantu dengan library external yaitu AForge dan juga dengan menggunakan Visual Studio 2022. 





Berikut merupakan code menggunakan bahasa C# nya 

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



        }

    }


Komentar

Postingan populer dari blog ini

Quiz PBKK A 2

PBKK A - Tugas 7

Quiz PBKK A - Quiz 1