private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (httpScreenStreamer == null)
                {
                    httpScreenStreamer = new HttpScreenStreamer();

                    httpScreenStreamer.StreamerStarted += HttpScreenStreamer_StreamerStarted;
                    httpScreenStreamer.StreamerStopped += HttpScreenStreamer_StreamerStopped;
                }

                var srcRect     = HttpGetCurrentScreen(); //currentScreen.Bounds;
                var _destWidth  = (int)httpDestWidthNumeric.Value;
                var _destHeight = (int)httpDestHeightNumeric.Value;

                var destSize = new Size(_destWidth, _destHeight);
                VideoCaptureType captureType = (VideoCaptureType)captureTypesComboBox.SelectedItem;


                HttpScreenStreamerArgs args = new HttpScreenStreamerArgs
                {
                    Addres        = httpAddrTextBox.Text,
                    Port          = (int)httpPortNumeric.Value,
                    CaptureRegion = srcRect,
                    Resolution    = destSize,

                    Fps          = (int)httpFpsNumeric.Value,
                    CaptureTypes = captureType,
                    CaptureMouse = true,
                    //UseDesktopDuplApi = (captureType == VideoCaptureType.DXGIDeskDupl),
                };

                httpScreenStreamer.Setup(args);

                httpScreenStreamer.Start();

                this.Cursor = Cursors.WaitCursor;

                this.Enabled = false;
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                MessageBox.Show(ex.ToString());
            }
        }
        public void Setup(HttpScreenStreamerArgs args)
        {
            logger.Debug("HttpScreenStreamer::Setup() " + args.ToString());

            if (state != MediaState.Closed)
            {
                throw new InvalidOperationException("Invalid state " + State);
            }

            errorCode = 0;

            //var srcRect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            //var destSize = new Size(1920, 1080);

            var srcRect  = args.CaptureRegion;
            var destSize = args.Resolution;

            var ratio      = srcRect.Width / (double)srcRect.Height;
            int destWidth  = destSize.Width;
            int destHeight = (int)(destWidth / ratio);

            if (ratio < 1)
            {
                destHeight = destSize.Height;
                destWidth  = (int)(destHeight * ratio);
            }

            destSize = new Size(destWidth, destHeight);
            var captureType = args.CaptureTypes;

            var captureProp = new ScreenCaptureProperties
            {
                CaptureType  = captureType,
                Fps          = (int)args.Fps,
                CaptureMouse = args.CaptureMouse,
                AspectRatio  = true,
                UseHardware  = false,
            };

            ScreenCaptureDevice captureParams = new ScreenCaptureDevice
            {
                CaptureRegion = srcRect,
                Resolution    = destSize,

                Properties = captureProp,

                //CaptureType = captureType,
                //Fps = (int)args.Fps,
                //CaptureMouse = args.CaptureMouse,
                //AspectRatio = true,
                //UseHardware = false,
            };


            if (captureType == VideoCaptureType.GDI ||
                captureType == VideoCaptureType.GDILayered ||
                captureType == VideoCaptureType.GDIPlus ||
                captureType == VideoCaptureType.Datapath)
            {// масштабируем на энкодере
                captureParams.Resolution = new Size(srcRect.Width, srcRect.Height);
            }

            VideoEncoderSettings encodingParams = new VideoEncoderSettings
            {
                EncoderFormat = VideoCodingFormat.JPEG,
                //Resolution = destSize,
                Width     = destSize.Width,
                Height    = destSize.Height,
                FrameRate = new MediaRatio(captureParams.Properties.Fps, 1),
                EncoderId = "mjpeg",
            };

            NetworkSettings networkParams = new NetworkSettings
            {
                RemoteAddr = args.Addres,
                RemotePort = args.Port,
            };

            try
            {
                httpScreenSource = new ScreenSource();
                httpScreenSource.Setup(captureParams);
                httpScreenSource.CaptureStopped += HttpScreenSource_CaptureStopped;

                httpStreamer = new VideoHttpStreamer(httpScreenSource);
                httpStreamer.Setup(encodingParams, networkParams);
                httpStreamer.StreamerStopped += HttpStreamer_StreamerStopped;

                state = MediaState.Initialized;
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                errorCode = 100503;

                Close();
                throw;
            }
        }