Beispiel #1
0
        public void UpdateResolution(SizeInt32 size)
        {
            if (size.Height <= 0 || size.Width <= 0)
            {
                return;
            }

            if (_framePool is not null && _session is not null)
            {
                // Unsubscribe from old framePool
                _framePool.FrameArrived -= OnFrameArrived;

                // Dispose old session and framePool
                _session.Dispose();
                _framePool.Dispose();
            }

            _canvasDevice = CanvasDevice.GetSharedDevice();

            // Create a frame pool with room for only 1 frame because we're getting a single frame, not a video.
            _framePool =
                Direct3D11CaptureFramePool.Create(_canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, size);

            _session = _framePool.CreateCaptureSession(GraphicsCaptureItem.CreateFromVisual(_capturedVisual));

            _framePool.FrameArrived += OnFrameArrived;

            _session.StartCapture();
        }
Beispiel #2
0
        //
        // Renders a the given <see cref="Visual"/> to a <see cref="CanvasBitmap"/>. If <paramref name="size"/> is not
        // specified, uses the size of <paramref name="visual"/>.
        //
        static async Task <CanvasBitmap> RenderVisualToBitmapAsync(Visual visual, SizeInt32?size = null)
        {
            // Get an object that enables capture from a visual.
            var graphicsItem = GraphicsCaptureItem.CreateFromVisual(visual);

            var canvasDevice = CanvasDevice.GetSharedDevice();

            var tcs = new TaskCompletionSource <CanvasBitmap>();

            // Create a frame pool with room for only 1 frame because we're getting a single frame, not a video.
            const int numberOfBuffers = 1;

            using (var framePool = Direct3D11CaptureFramePool.Create(
                       canvasDevice,
                       DirectXPixelFormat.B8G8R8A8UIntNormalized,
                       numberOfBuffers,
                       size ?? graphicsItem.Size))
            {
                void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
                {
                    using (var frame = sender.TryGetNextFrame())
                    {
                        tcs.SetResult(frame != null
                            ? CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, frame.Surface)
                            : null);
                    }
                }

                using (var session = framePool.CreateCaptureSession(graphicsItem))
                {
                    framePool.FrameArrived += OnFrameArrived;

                    // Start capturing. The FrameArrived event will occur shortly.
                    session.StartCapture();

                    // Wait for the frame to arrive.
                    var result = await tcs.Task;

                    // !!!!!!!! NOTE !!!!!!!!
                    // This thread is now running inside the OnFrameArrived callback method.

                    // Unsubscribe now that we have captured the frame.
                    framePool.FrameArrived -= OnFrameArrived;

                    // Yield to allow the OnFrameArrived callback to unwind so that it is safe to
                    // Dispose the session and framepool.
                    await Task.Yield();
                }
            }

            return(await tcs.Task);
        }
        private void MainWebView_Loaded(object sender, RoutedEventArgs e)
        {
            var compositor = Window.Current.Compositor;
            var visual     = ElementCompositionPreview.GetElementVisual(MainWebView);

            var size = new SizeInt32()
            {
                Width  = (int)MainWebView.ActualWidth,
                Height = (int)MainWebView.ActualHeight
            };

            var item = GraphicsCaptureItem.CreateFromVisual(visual);

            _capture = new SimpleCapture(_device, item, size);
            var surface = _capture.CreateSurface(compositor);

            _visual = compositor.CreateSpriteVisual();
            _visual.RelativeSizeAdjustment = Vector2.One;
            _visual.Brush = compositor.CreateSurfaceBrush(surface);

            ElementCompositionPreview.SetElementChildVisual(VisualGrid, _visual);
            _capture.StartCapture();
        }