Tugas cam
Nama: Muhammad Arkan Karindra Darvesh
Source code :
<div style="background-color: blue; color: white; padding: 10px;">
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing;
using System.Drawing.Imaging;
namespace WebCamApp
{
public partial class Form1 : Form
{
private FilterInfoCollection captureDevice;
private VideoCaptureDevice captureDeviceVideo;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo deviceList in captureDevice)
{
comboBoxWebcam.Items.Add(deviceList.Name);
}
comboBoxWebcam.SelectedIndex = 0;
captureDeviceVideo = new VideoCaptureDevice();
}
private void btnStart_Click(object sender, EventArgs e)
{
if (captureDeviceVideo.IsRunning)
{
captureDeviceVideo.SignalToStop();
captureDeviceVideo.WaitForStop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
}
captureDeviceVideo = new VideoCaptureDevice(captureDevice[comboBoxWebcam.SelectedIndex].MonikerString);
captureDeviceVideo.NewFrame += new NewFrameEventHandler(CaptureDeviceVideo_NewFrame);
captureDeviceVideo.Start();
}
private void CaptureDeviceVideo_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void btnCapture_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void btnSaveImg_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 btnExit_Click(object sender, EventArgs e)
{
if (captureDeviceVideo.IsRunning)
{
captureDeviceVideo.SignalToStop();
captureDeviceVideo.WaitForStop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox2.Image = null;
pictureBox2.Invalidate();
}
Application.Exit(null);
}
}
}
</div>
Komentar
Posting Komentar