Example #1
0
        private void VideoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            Invoke((MethodInvoker) delegate
            {
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                }
                Bitmap clone = (Bitmap)eventArgs.Frame.Clone();

                try
                {
                    Rectangle rect = new Rectangle(0, 0, eventArgs.Frame.Width, eventArgs.Frame.Height);
                    System.Drawing.Imaging.BitmapData bmpData = eventArgs.Frame.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, eventArgs.Frame.PixelFormat);
                    IntPtr ptr = bmpData.Scan0;
                    var bytes  = _encoder.EncodeFrame(ptr);
                    eventArgs.Frame.UnlockBits(bmpData);
                    if (bytes != null && bytes.Length > 0)
                    {
                        _writer.Write(bytes);
                        _socket.SendTo(bytes, _endPoint);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                pictureBox1.Image = clone;
            });
        }