Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using AForge.Controls;
- using System.Windows.Forms;
- using AForge.Video.DirectShow;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- namespace WebCamTest
- {
- public partial class WebCamTest : Form
- {
- VideoSourcePlayer videoSourcePlayer;
- FilterInfoCollection videoDevices;
- public WebCamTest()
- {
- InitializeComponent();
- videoSourcePlayer = new VideoSourcePlayer();
- try
- {
- videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- if (videoDevices.Count == 0)
- throw new ApplicationException();
- foreach (FilterInfo device in videoDevices)
- {
- devicesCombo.Items.Add(device.Name);
- }
- }
- catch (ApplicationException)
- {
- devicesCombo.Items.Add("No local capture devices");
- devicesCombo.Enabled = false;
- takePictureBtn.Enabled = false;
- }
- devicesCombo.SelectedIndex = 0;
- VideoCaptureDevice videoCaptureSource = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
- videoSourcePlayer.VideoSource = videoCaptureSource;
- videoSourcePlayer.Start();
- }
- private void takePictureBtn_Click(object sender, EventArgs e)
- {
- DateTime time = DateTime.Now;
- string format = "MMM ddd d HH mm yyyy";
- String strFilename = "Capture-" + time.ToString(format) + ".jpg";
- if (videoSourcePlayer.IsRunning)
- {
- using (Bitmap picture = videoSourcePlayer.GetCurrentVideoFrame())
- using (Bitmap pictureResized = ResizeBitmap(picture, 320, 240))
- {
- pictureResized.Save(strFilename, ImageFormat.Jpeg);
- labelSaved.Text = "Capture Saved : " + strFilename;
- }
- }
- }
- private Bitmap ResizeBitmap(Bitmap originalBitmap, int newWidth, int newHeight)
- {
- int sourceWidth = originalBitmap.Width;
- int sourceHeight = originalBitmap.Height;
- int sourceX = 0;
- int sourceY = 0;
- int destX = 0;
- int destY = 0;
- float nPercent = 0;
- float nPercentW = 0;
- float nPercentH = 0;
- nPercentW = ((float)newWidth / (float)sourceWidth);
- nPercentH = ((float)newHeight / (float)sourceHeight);
- if (nPercentH < nPercentW)
- {
- nPercent = nPercentH;
- destX = Convert.ToInt16((newWidth - (sourceWidth * nPercent)) / 2);
- }
- else
- {
- nPercent = nPercentW;
- destY = Convert.ToInt16((newHeight - (sourceHeight * nPercent)) / 2);
- }
- int destWidth = (int)(sourceWidth * nPercent);
- int destHeight = (int)(sourceHeight * nPercent);
- Bitmap bmPhoto = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(originalBitmap.HorizontalResolution, originalBitmap.VerticalResolution);
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
- grPhoto.Clear(Color.Black);
- grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
- grPhoto.DrawImage(
- originalBitmap,
- new Rectangle(destX, destY, destWidth, destHeight),
- new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
- GraphicsUnit.Pixel);
- grPhoto.Dispose();
- return bmPhoto;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- videoSourcePlayer.SignalToStop();
- videoSourcePlayer.WaitForStop();
- videoDevices = null;
- }
- private void devicesCombo_SelectedIndexChanged(object sender, EventArgs e)
- {
- videoSourcePlayer.SignalToStop();
- videoSourcePlayer.WaitForStop();
- VideoCaptureDevice videoCaptureSource = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
- videoSourcePlayer.VideoSource = videoCaptureSource;
- videoSourcePlayer.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement