PBKK : Pertemuan 3
NRP : 5025211195
Kelas : PBKK A
Kelas : PBKK A
Halo semua! pada kesempatan kali ini, saya ingin membagikan pengalaman dalam membuat Project Cam Capture App (Pengambilan Gambar) untuk Desktop. Projek ini dirancang menggunakan .NET Framework dan bahasa C# serta dibantu dengan library external yaitu AForge dan juga dengan menggunakan tools Visual Studio 2022.
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);
}
}
Comments
Post a Comment