2 using System.Collections.Generic;
 
    8 using AForge.Video.DirectShow;
 
   20         private VideoCaptureDevice videoSource;
 
   21         private readonly Dictionary<string, string> videoSources = 
new Dictionary<string, string>();
 
   22         private readonly Object pulseObject = 
new object();
 
   24         readonly SolidBrush shadowBrushDarkRed = 
new SolidBrush(Color.FromArgb(100, Color.Firebrick));
 
   25         readonly SolidBrush shadowBrushBlue = 
new SolidBrush(Color.FromArgb(25, Color.Blue));
 
   26         readonly SolidBrush shadowBrushOrange = 
new SolidBrush(Color.FromArgb(75, Color.Orange));
 
   27         readonly SolidBrush shadowBrushRed = 
new SolidBrush(Color.FromArgb(75, Color.Red));
 
   37         public bool CaptureNextImage { 
get; 
private set; }
 
   41         private Image nextImage;
 
   48         public bool FilterLaser { 
get; set; }
 
   55         public bool ShowLaserLine { 
get; set; }
 
   62         public float LaserLeftPosition { 
get; set; }
 
   69         public float LaserRightPosition { 
get; set; }
 
   76         public bool LaserMotorEnabled { 
get; set; }
 
   86                 this.InitializeComponent();
 
   96         private void CameraControl_Load(
object sender, EventArgs e)
 
  101             this.InitVideoSources();
 
  105         private void InitVideoSources()
 
  107             foreach (FilterInfo filterInfo 
in new FilterInfoCollection(FilterCategory.VideoInputDevice))
 
  109                 this.videoSources.Add(filterInfo.Name, filterInfo.MonikerString);
 
  121             if (!this.videoSources.ContainsKey(source))
 
  123                 this.videoSources.Clear();
 
  124                 this.InitVideoSources();
 
  125                 if (!this.videoSources.ContainsKey(source))
 
  131             this.videoSource = 
new VideoCaptureDevice(this.videoSources[source]);
 
  133             if (this.videoSource.VideoCapabilities.Length > 0)
 
  135                 VideoCapabilities max = this.videoSource.VideoCapabilities.OrderBy(p => p.FrameSize.Width).Last();
 
  136                 this.videoSource.VideoResolution = max;
 
  139             this.videoSource.NewFrame += this.videoSource_NewFrame;
 
  140             this.videoSource.Start();
 
  150         private void videoSource_NewFrame(
object sender, NewFrameEventArgs eventArgs)
 
  159                 bool setTrap = 
false;
 
  160                 if (this.CaptureNextImage)
 
  163                     this.CaptureNextImage = 
false;
 
  166                 Bitmap image = (Bitmap) eventArgs.Frame.Clone();
 
  170                 if (this.FilterLaser)
 
  174                     if (this.ShowLaserLine)
 
  178                         if (laserPosition > 0.0f)
 
  182                                 Bitmap tempBitmap = 
new Bitmap(grayImage.Width, grayImage.Height);
 
  183                                 using (Graphics graphics = Graphics.FromImage(tempBitmap))
 
  185                                     graphics.DrawImage(grayImage, 0, 0);
 
  186                                     graphics.FillRectangle(this.shadowBrushDarkRed,
 
  187                                         new RectangleF(laserPosition - 2, 0, 4, grayImage.Height));
 
  189                                 this.pictureBoxCameraImage.Image = tempBitmap;
 
  197                             this.pictureBoxCameraImage.Image = grayImage;
 
  202                         this.pictureBoxCameraImage.Image = grayImage;
 
  207                     using (Graphics graphics = Graphics.FromImage(image))
 
  209                         int middle = image.Width/2;
 
  214                         RectangleF turntableRectangleF = 
new RectangleF
 
  216                             X = middle - turntableXRadius,
 
  217                             Width = 2*turntableXRadius,
 
  218                             Y = image.Height*0.75f,
 
  219                             Height = turntableXRadius/1.66f/((float)image.Width/(
float)image.Height)
 
  222                         graphics.FillEllipse(this.shadowBrushBlue, turntableRectangleF);
 
  227                         if (this.LaserMotorEnabled)
 
  229                             graphics.FillRectangle(this.shadowBrushOrange,
 
  230                                 new RectangleF(this.LaserLeftPosition - 2, 0, 4, image.Height));
 
  231                             graphics.FillRectangle(this.shadowBrushRed,
 
  232                                 new RectangleF(this.LaserRightPosition - 2, 0, 4, image.Height));
 
  235                         if (this.ShowLaserLine)
 
  237                             float laserPosition = TransformImage.GetLaserLinePosition(image,
 
  240                             graphics.FillRectangle(this.shadowBrushDarkRed,
 
  241                                 new RectangleF(laserPosition - 2, 0, 4, image.Height));
 
  245                     this.pictureBoxCameraImage.Image = image;
 
  250                 this.nextImage = (
Image)eventArgs.Frame.Clone();
 
  251                 lock (this.pulseObject)
 
  252                     Monitor.Pulse(this.pulseObject);
 
  267             if (this.videoSource == null || !this.videoSource.IsRunning) 
 
  270             this.videoSource.SignalToStop();
 
  271             this.videoSource = null;
 
  282             lock (this.pulseObject)
 
  284                 this.CaptureNextImage = 
true;
 
  285                 Monitor.Wait(this.pulseObject);
 
  287             lock (this.pulseObject)
 
  289                 this.CaptureNextImage = 
true;
 
  290                 Monitor.Wait(this.pulseObject);
 
  292             return this.nextImage;
 
void StartImaging(string source)
Starts the imaging. 
 
CameraControl()
Initializes a new instance of the CameraControl class. 
 
void StopImaging()
Stops the imaging. 
 
Control for accessing a camera 
 
Class for creating a singleton for a generic class 
 
static T Instance
Gets the instance. 
 
Image GetNextPicture()
Gets the next picture.