Latihan Membuat Aplikasi Dekstop #2 - Webcam App
21 Februari 2023
Latihan membuat aplikasi dekstop kedua yaitu membuat aplikasi webcam capture menggunakan .NET Framework dan Aforge .Net dengan bahasa C#
Referensi yang digunakan berasal dari youtube channel Pintar Ngoding
Fitur yang ada pada aplikasi webcam capture ini antara lain:
- Memilih device capture yang akan digunakan
- Start device capture
- Capture Image
- Save image
Repository lengkap aplikasi dapat diakses melalui Github
Demo Aplikasi
Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AForge; | |
using AForge.Video; | |
using AForge.Video.DirectShow; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
namespace WebcamCaptureApp | |
{ | |
public partial class WebcamCaptureApp : Form | |
{ | |
private FilterInfoCollection captureDevice; | |
private VideoCaptureDevice videoSource; | |
public WebcamCaptureApp() | |
{ | |
InitializeComponent(); | |
} | |
private void WebcamCaptureApp_Load(object sender, EventArgs e) | |
{ | |
captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice); | |
foreach(FilterInfo deviceList in captureDevice) | |
{ | |
comboBox1.Items.Add(deviceList.Name); | |
} | |
comboBox1.SelectedIndex = 0; | |
videoSource = new VideoCaptureDevice(); | |
} | |
private void BtnStart_Click(object sender, EventArgs e) | |
{ | |
if (videoSource.IsRunning) | |
{ | |
videoSource.SignalToStop(); | |
videoSource.WaitForStop(); | |
pictureBox1.Image = null; | |
pictureBox2.Invalidate(); | |
} | |
videoSource = new VideoCaptureDevice(captureDevice[comboBox1.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 BtnCapture_Click(object sender, EventArgs e) | |
{ | |
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); | |
} | |
private void BtnSave_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); | |
} | |
} | |
} | |
} |
Nama : Arief Badrus Sholeh
NRP : 5025201228
Kelas : PBKK B
Tahun : 2022/2023
Komentar
Posting Komentar