/// <summary>
            /// Retrieves the next frame and copies the texture into the _desktopImageTexture.
            /// </summary>
            /// <param name="frameInformation"></param>
            /// <returns></returns>
            public bool TryRetrieveFrame(out OutduplFrameInfo frameInformation)
            {
                IDXGIResource desktopResource = null;

                try
                {
                    _outputDuplication.AcquireNextFrame(100, out frameInformation, out desktopResource);
                    using var tempTexture = desktopResource.QueryInterface <ID3D11Texture2D>();
                    _d3dDevice.ImmediateContext.CopyResource(_desktopImageTexture, tempTexture);
                    return(true);
                }
                catch (SharpGenException ex)
                {
                    if (ex.ResultCode.Failure && ex.Descriptor.NativeApiCode != "DXGI_ERROR_ACCESS_LOST" && ex.Descriptor.NativeApiCode != "DXGI_ERROR_WAIT_TIMEOUT")
                    {
                        throw new DesktopDuplicationException("Failed to acquire next frame.");
                    }
                }
                finally
                {
                    if (desktopResource != null)
                    {
                        desktopResource.Dispose();
                    }
                }

                frameInformation = new OutduplFrameInfo();
                return(false);
            }
Example #2
0
        /// <summary>
        ///     Indicates that the application is ready to process the next desktop image.
        /// </summary>
        /// <param name="timeoutInMilliseconds">
        ///     The time-out interval, in milliseconds. This interval specifies the amount of time that this method waits for a new
        ///     frame before it returns to the caller. This method returns if the interval elapses, and a new desktop image is not
        ///     available.
        ///     For more information about the time-out interval, see Remarks.
        /// </param>
        /// <param name="frameInfo">
        ///     A out variable that receives the <seealso cref="DXGIOutDuplFrameInfo" />
        ///     structure that describes timing and presentation statistics for a frame.
        /// </param>
        /// <param name="desktopResource">
        ///     A out variable that receives the <seealso cref="IDXGIResource" /> interface of
        ///     the surface that contains the desktop bitmap.
        /// </param>
        /// <returns></returns>
        /// <remarks>
        ///     When AcquireNextFrame returns successfully, the calling application can access the desktop image that
        ///     <seealso cref="IDXGIOutputDuplication.AcquireNextFrame" /> returns in the variable at
        ///     <paramref name="desktopResource" />. If the caller
        ///     specifies a zero time-out interval in
        ///     the <paramref name="timeoutInMilliseconds" /> parameter, <seealso cref="IDXGIOutputDuplication.AcquireNextFrame" />
        ///     verifies whether there
        ///     is a new desktop image available,
        ///     returns immediately, and indicates its outcome with the return value. If the caller specifies an INFINITE time-out
        ///     interval in the <paramref name="timeoutInMilliseconds" /> parameter, the time-out interval never elapses.
        ///     <b>
        ///         Note: You cannot cancel the wait that you specified in the <paramref name="timeoutInMilliseconds" /> parameter.
        ///         Therefore, if you
        ///         must periodically check for other conditions (for example, a terminate signal), you should specify a
        ///         non-INFINITE time-out interval. After the time-out interval elapses, you can check for these other conditions
        ///         and then call <seealso cref="IDXGIOutputDuplication.AcquireNextFrame" /> again to wait for the next frame.
        ///     </b>
        ///     <seealso cref="IDXGIOutputDuplication.AcquireNextFrame" /> acquires a new desktop frame when the operating system
        ///     either updates the
        ///     desktop bitmap image or
        ///     changes the shape or position of a hardware pointer. The new frame that
        ///     <seealso cref="IDXGIOutputDuplication.AcquireNextFrame" />
        ///     acquires might have only
        ///     the desktop image updated, only the pointer shape or position updated, or both.
        /// </remarks>
        public int AcquireNextFrame(uint timeoutInMilliseconds, out DXGIOutDuplFrameInfo frameInfo,
                                    out IDXGIResource desktopResource)
        {
            int result = GetMethodDelegate <DXGIAcquireNextFrameDelegate>()
                         .Invoke(this, timeoutInMilliseconds, out frameInfo, out IntPtr resourcePtr);

            desktopResource = result == 0 ? new DXGIResource(resourcePtr) : null;
            return(result);
        }
    public IDXGIDecodeSwapChain CreateDecodeSwapChainForCompositionSurfaceHandle(
        IUnknown device,
        IntPtr surface,
        IDXGIResource yuvDecodeBuffers)
    {
        // Reserved for future use (https://docs.microsoft.com/it-it/windows/desktop/api/dxgi1_3/ns-dxgi1_3-dxgi_decode_swap_chain_desc)
        var description = new DecodeSwapChainDescription
        {
            Flags = 0
        };

        return(CreateDecodeSwapChainForCompositionSurfaceHandle(device, surface, description, yuvDecodeBuffers, null));
    }
        public IDXGIDecodeSwapChain CreateDecodeSwapChainForCompositionSurfaceHandle(
            IUnknown device,
            IntPtr surface,
            IDXGIResource yuvDecodeBuffers)
        {
            Guard.NotNull(device, nameof(device));
            Guard.IsTrue(surface != IntPtr.Zero, nameof(surface), "Invalid surface handle");
            Guard.NotNull(yuvDecodeBuffers, nameof(yuvDecodeBuffers));

            // Reserved for future use (https://docs.microsoft.com/it-it/windows/desktop/api/dxgi1_3/ns-dxgi1_3-dxgi_decode_swap_chain_desc)
            var description = new DecodeSwapChainDescription
            {
                Flags = 0
            };

            return(CreateDecodeSwapChainForCompositionSurfaceHandle(device, surface, description, yuvDecodeBuffers, null));
        }