Example #1
0
        } = true;                                        //GDI is supported everywhere

        public ScreenInfo Initialize(ComponentOptions componentOptions)
        {
            var options = componentOptions.To <GdiOptions>();
            var screen  = Screen.AllScreens[options.Monitor.Value];

            _screenBounds = screen.Bounds;

            _currentImage        = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format24bppRgb);
            _screenDeviceContext = NativeMethods.CreateDC("DISPLAY", null, null, IntPtr.Zero);

            if (_screenDeviceContext == IntPtr.Zero)
            {
                throw new InvalidOperationException("Creating device context for display failed.");
            }

            return(new ScreenInfo(screen.Bounds.Width, screen.Bounds.Height));
        }
Example #2
0
        public ScreenInfo Initialize(ComponentOptions componentOptions)
        {
            var options = componentOptions.To <DesktopDuplicationOptions>();
            var monitor = options.Monitor.Value;

            Adapter1 adapter;

            try
            {
                using (var factory = new Factory1())
                    adapter = GetBestAdapter(factory);
            }
            catch (SharpDXException e)
            {
                throw new InvalidOperationException("Could not find the specified graphics card adapter.", e);
            }

            Output output;

            using (adapter)
            {
                _captureDevice = new Device(adapter);

                try
                {
                    output = adapter.GetOutput(monitor);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Could not find the specified output device.", e);
                }
            }

            using (output)
                using (var output1 = output.QueryInterface <Output1>())
                {
                    _outputDesc  = output.Description;
                    _textureDesc = new Texture2DDescription
                    {
                        CpuAccessFlags    = CpuAccessFlags.Read,
                        BindFlags         = BindFlags.None,
                        Format            = Format.B8G8R8A8_UNorm,
                        Width             = GetWidth(_outputDesc.DesktopBounds),
                        Height            = GetHeight(_outputDesc.DesktopBounds),
                        OptionFlags       = ResourceOptionFlags.None,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        SampleDescription = { Count = 1, Quality = 0 },
                        Usage             = ResourceUsage.Staging
                    };

                    try
                    {
                        _deskDupl = output1.DuplicateOutput(_captureDevice);
                    }
                    catch (SharpDXException ex) when(ex.ResultCode.Code == ResultCode.NotCurrentlyAvailable.Result.Code)
                    {
                        throw new InvalidOperationException(
                                  "There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.");
                    }

                    return(new ScreenInfo(_textureDesc.Width, _textureDesc.Height));
                }
        }