public static DXGI_OUTDUPL_DESC GetDesc(this IDXGIOutputDuplication duplication)
        {
            if (duplication == null)
            {
                throw new ArgumentNullException(nameof(duplication));
            }

#pragma warning disable CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
            duplication.GetDesc(out var desc);
#pragma warning restore CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
            return(desc);
        }
        public static DXGI_OUTDUPL_DESC GetDesc(this IDXGIOutputDuplication duplication)
        {
            if (duplication == null)
            {
                throw new ArgumentNullException(nameof(duplication));
            }

            var desc = new DXGI_OUTDUPL_DESC();

            duplication.GetDesc(out desc);
            return(desc);
        }
        private void StartDesktopDuplicator(int adapterId, int outputId)
        {
            var adapter = factory.GetAdapter1(adapterId);

            D3D11.D3D11CreateDevice(adapter, DriverType.Unknown, DeviceCreationFlags.None, s_featureLevels, out device);

            var output  = adapter.GetOutput(outputId);
            var output1 = output.QueryInterface <IDXGIOutput1>();

            var bounds = output1.Description.DesktopCoordinates;
            var width  = bounds.Right - bounds.Left;
            var height = bounds.Bottom - bounds.Top;

            var textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width / 2,
                Height            = height / 2,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = Usage.Staging
            };

            stagingTexture = device.CreateTexture2D(textureDesc);

            var smallerTextureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.None,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.GenerateMips,
                MipLevels         = 4,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = Usage.Default
            };

            smallerTexture     = device.CreateTexture2D(smallerTextureDesc);
            smallerTextureView = device.CreateShaderResourceView(smallerTexture);

            duplication = output1.DuplicateOutput(device);
        }