Advertisement
Guest User

Untitled

a guest
Mar 25th, 2018
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using AForge.Controls;
  4. using System.Windows.Forms;
  5. using AForge.Video.DirectShow;
  6. using System.Drawing.Imaging;
  7. using System.Drawing.Drawing2D;
  8.  
  9. namespace WebCamTest
  10. {
  11.     public partial class WebCamTest : Form
  12.     {
  13.         VideoSourcePlayer videoSourcePlayer;
  14.         FilterInfoCollection videoDevices;
  15.  
  16.         public WebCamTest()
  17.         {
  18.             InitializeComponent();
  19.  
  20.             videoSourcePlayer = new VideoSourcePlayer();
  21.  
  22.             try
  23.             {
  24.                 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  25.  
  26.                 if (videoDevices.Count == 0)
  27.                     throw new ApplicationException();
  28.  
  29.                 foreach (FilterInfo device in videoDevices)
  30.                 {
  31.                     devicesCombo.Items.Add(device.Name);
  32.                 }
  33.             }
  34.             catch (ApplicationException)
  35.             {
  36.                 devicesCombo.Items.Add("No local capture devices");
  37.                 devicesCombo.Enabled = false;
  38.                 takePictureBtn.Enabled = false;
  39.             }
  40.  
  41.             devicesCombo.SelectedIndex = 0;
  42.  
  43.             VideoCaptureDevice videoCaptureSource = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
  44.             videoSourcePlayer.VideoSource = videoCaptureSource;
  45.             videoSourcePlayer.Start();
  46.         }
  47.  
  48.         private void takePictureBtn_Click(object sender, EventArgs e)
  49.         {
  50.             DateTime time = DateTime.Now;
  51.             string format = "MMM ddd d HH mm yyyy";
  52.             String strFilename = "Capture-" + time.ToString(format) + ".jpg";
  53.             if (videoSourcePlayer.IsRunning)
  54.             {
  55.                 using (Bitmap picture = videoSourcePlayer.GetCurrentVideoFrame())
  56.                 using (Bitmap pictureResized = ResizeBitmap(picture, 320, 240))
  57.                 {
  58.                     pictureResized.Save(strFilename, ImageFormat.Jpeg);
  59.                     labelSaved.Text = "Capture Saved : " + strFilename;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private Bitmap ResizeBitmap(Bitmap originalBitmap, int newWidth, int newHeight)
  65.         {
  66.             int sourceWidth = originalBitmap.Width;
  67.             int sourceHeight = originalBitmap.Height;
  68.             int sourceX = 0;
  69.             int sourceY = 0;
  70.             int destX = 0;
  71.             int destY = 0;
  72.  
  73.             float nPercent = 0;
  74.             float nPercentW = 0;
  75.             float nPercentH = 0;
  76.  
  77.             nPercentW = ((float)newWidth / (float)sourceWidth);
  78.             nPercentH = ((float)newHeight / (float)sourceHeight);
  79.  
  80.             if (nPercentH < nPercentW)
  81.             {
  82.                 nPercent = nPercentH;
  83.                 destX = Convert.ToInt16((newWidth - (sourceWidth * nPercent)) / 2);
  84.             }
  85.             else
  86.             {
  87.                 nPercent = nPercentW;
  88.                 destY = Convert.ToInt16((newHeight - (sourceHeight * nPercent)) / 2);
  89.             }
  90.  
  91.             int destWidth = (int)(sourceWidth * nPercent);
  92.             int destHeight = (int)(sourceHeight * nPercent);
  93.  
  94.             Bitmap bmPhoto = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
  95.             bmPhoto.SetResolution(originalBitmap.HorizontalResolution, originalBitmap.VerticalResolution);
  96.  
  97.             Graphics grPhoto = Graphics.FromImage(bmPhoto);
  98.             grPhoto.Clear(Color.Black);
  99.             grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
  100.  
  101.             grPhoto.DrawImage(
  102.                 originalBitmap,
  103.                 new Rectangle(destX, destY, destWidth, destHeight),
  104.                 new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
  105.                 GraphicsUnit.Pixel);
  106.  
  107.             grPhoto.Dispose();
  108.  
  109.             return bmPhoto;
  110.         }
  111.  
  112.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  113.         {
  114.             videoSourcePlayer.SignalToStop();
  115.             videoSourcePlayer.WaitForStop();
  116.             videoDevices = null;
  117.         }
  118.  
  119.         private void devicesCombo_SelectedIndexChanged(object sender, EventArgs e)
  120.         {
  121.             videoSourcePlayer.SignalToStop();
  122.             videoSourcePlayer.WaitForStop();
  123.             VideoCaptureDevice videoCaptureSource = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
  124.             videoSourcePlayer.VideoSource = videoCaptureSource;
  125.             videoSourcePlayer.Start();
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement