Example #1
0
        public async Task <SoftwareBitmap> GetCaptureAsync()
        {
            var d2d   = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0);
            var frame = Direct3D11Helper.CreateDirect3DSurfaceFromSharpDXTexture(d2d);

            return(await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame));
        }
Example #2
0
        private DirectXVideoSample?GetNextFrame()
        {
            currentFrame?.Dispose();
            frameEvent.Reset();

            var signaledEvent = events[WaitHandle.WaitAny(events)];

            if (signaledEvent == closedEvent)
            {
                // Safe to cleanup non-persistent resources as this method will return and the code below no longer references them
                canCleanupNonPersistentResourcesEvent.Set();
                return(null);
            }

            using (new MultithreadLock(multithread))
            {
                if (currentFrame == null)
                {
                    return(null);
                }

                // Copy the captured frame from the framepool to a useable texture
                using var sourceTexture = Direct3D11Helper.CreateSharpDXTexture2D(currentFrame.Surface);
                if (sourceTexture == null)
                {
                    return(null);
                }

                var description = sourceTexture.Description;
                description.Usage          = ResourceUsage.Default;
                description.BindFlags      = BindFlags.ShaderResource | BindFlags.RenderTarget;
                description.CpuAccessFlags = CpuAccessFlags.None;
                description.OptionFlags    = ResourceOptionFlags.None;

                using var copyTexture = new Texture2D(sharpDXDevice, description);
                var width  = Math.Clamp(currentFrame.ContentSize.Width, 0, currentFrame.Surface.Description.Width);
                var height = Math.Clamp(currentFrame.ContentSize.Height, 0, currentFrame.Surface.Description.Height);

                var region = new ResourceRegion(0, 0, 0, width, height, 1);

                sharpDXDevice.ImmediateContext.CopyResource(blankTexture, copyTexture);
                sharpDXDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, copyTexture, 0);

                var surface = Direct3D11Helper.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture);
                if (surface == null)
                {
                    return(null);
                }

                return(new DirectXVideoSample
                {
                    Surface = surface
                });
            }
        }