private async Task StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _canvasDevice = new CanvasDevice();

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();

            await Task.Delay(500);

            var frame = _framePool.TryGetNextFrame();

            await ProcessFrame(frame);

            StopCapture();
        }
        private async void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            _currentFrame = sender.TryGetNextFrame();


            BarcodeReader reader = new BarcodeReader();

            reader.AutoRotate              = true;
            reader.Options.TryHarder       = true;
            reader.Options.PureBarcode     = false;
            reader.Options.PossibleFormats = new List <BarcodeFormat>();
            reader.Options.PossibleFormats.Add(BarcodeFormat.QR_CODE);

            var bitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(_currentFrame.Surface).AsTask();

            var result = reader.Decode(bitmap);

            if (!string.IsNullOrEmpty(result?.Text) && (result.Text.StartsWith("suavekeys|expression") || result.Text.StartsWith("suavekeys|gesture")))
            {
                Debug.WriteLine("WOOHOO WE FOUND A CODE");
                if (!_isSending)
                {
                    _isSending = true;
                    var command = result.Text.Split('|')[2];
                    await _suaveKeysService.SendCommandAsync(command);

                    _isSending = false;
                }
            }
            _frameEvent.Set();
        }
        public Texture2D TryGetNextFrameAsTexture2D(Device device)
        {
            using var frame = _captureFramePool?.TryGetNextFrame();
            if (frame == null)
            {
                return(null);
            }

            // ReSharper disable once SuspiciousTypeConversion.Global
            var surfaceDxgiInterfaceAccess = (IDirect3DDxgiInterfaceAccess)frame.Surface;
            var pResource = surfaceDxgiInterfaceAccess.GetInterface(new Guid("dc8e63f3-d12b-4952-b47b-5e45026a862d"));

            using var surfaceTexture = new Texture2D(pResource); // shared resource
            var texture2dDescription = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Height            = surfaceTexture.Description.Height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                Width             = surfaceTexture.Description.Width
            };
            var texture2d = new Texture2D(device, texture2dDescription);

            device.ImmediateContext.CopyResource(surfaceTexture, texture2d);

            return(texture2d);
        }
Example #4
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            StopCapture();
            _item      = item;
            _lastSize  = _item.Size;
            _swapChain = new CanvasSwapChain(_canvasDevice, _item.Size.Width, _item.Size.Height, 96);

            swapChain.SwapChain = _swapChain;

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                60,                                        // Number of frames
                _item.Size);                               // Size of the buffers
            _session = _framePool.CreateCaptureSession(_item);
            _framePool.FrameArrived += (s, a) =>
            {
                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };
            _item.Closed += (s, a) =>
            {
                StopCapture();
            };
            _session.StartCapture();
        }
Example #5
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    // The thing we have been capturing has changed size.
                    // We need to resize our swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate our frame pool.
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.ResizeBuffers(_lastSize.Width, _lastSize.Height);
                }

                using (var bitmap = CanvasBitmap.CreateFromDirect3D11Surface(_device, frame.Surface))
                    using (var drawingSession = _swapChain.CreateDrawingSession(Colors.Transparent))
                    {
                        drawingSession.DrawImage(bitmap);
                    }
            } // retire the frame

            _swapChain.Present();

            if (newSize)
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
Example #6
0
        private async void _onFrameArraved()
        {
            TimeSpan _lastTimeStamp = _startTime;

            while (_capturing)
            {
                QueryPerformanceCounter(out long start);
                using (var frame = _framePool.TryGetNextFrame())
                {
                    if (frame == null)
                    {
                        continue;
                    }
                    using (var bitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame.Surface, BitmapAlphaMode.Premultiplied))
                    {
                        _processBitmap(bitmap, frame.SystemRelativeTime - _lastTimeStamp);
                        _lastTimeStamp = frame.SystemRelativeTime;
                    }
                }
                QueryPerformanceCounter(out long end);
                var spendTime = TimeSpan.FromTicks(end - start);
                if (spendTime < TimeSpan.FromMilliseconds(41))
                {
                    await Task.Delay((TimeSpan.FromMilliseconds(41) - spendTime).Milliseconds);
                }
            }
            _seesion.Dispose();
            _framePool.Dispose();
            _mediaEncoder.CloseVideoWriter();
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.Resize(
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        2,
                        _lastSize);
                }

                using (var backBuffer = _swapChain.GetBuffer(0))
                    using (var lockSession = _multithread.Lock())
                    {
                        _deviceContext.CopyResource(backBuffer, frame.Surface);
                    }
            }

            _swapChain.Present();

            if (newSize)
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
Example #8
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;



            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != lastSize.Width ||
                    frame.ContentSize.Height != lastSize.Height)
                {
                    // The thing we have been capturing has changed size.
                    // We need to resize the swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate the frame pool.
                    newSize  = true;
                    lastSize = frame.ContentSize;

                    swapChain.ResizeBuffers(
                        2,
                        lastSize.Width,
                        lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }

                if (!isWait)
                {
                    if (isStartCapture)
                    {
                        using (var backBuffer = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                            using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                            {
                                //d3dDevice.ImmediateContext.CopyResource(bitmap, backBuffer);
                                CopyBitmap(frame.Surface, bitmap, frame.ContentSize.Width, frame.ContentSize.Height);

                                Console.WriteLine("Capture");

                                bitmap.Dispose();
                            };

                        isStartCapture = false;
                    }
                }

                //using (var backBuffer = swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0))
            } // Retire the frame.

            swapChain.Present(0, SharpDX.DXGI.PresentFlags.None);

            if (newSize)
            {
                framePool.Recreate(
                    device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    lastSize);
            }
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            _frameCounter++;
            System.Diagnostics.Debug.WriteLine("Frame Arrived Here 2: " + _frameCounter);
            Direct3D11CaptureFrame frame = sender.TryGetNextFrame();

            DisplayFrame(frame);
            SetResult(frame);
        }
Example #10
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            using (var frame = sender.TryGetNextFrame())
            {
                var needsReset     = false;
                var recreateDevice = false;

                if ((frame.ContentSize.Width != _lastSize.Width) || (frame.ContentSize.Height != _lastSize.Height))
                {
                    needsReset = true;
                    _lastSize  = frame.ContentSize;
                }

                try
                {
                    using (var backBuffer = _swapChain.GetBackBuffer <Texture2D>(0))
                    {
                        using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                        {
                            // copy current surface to backbuffer
                            _d3dDevice.ImmediateContext.CopyResource(bitmap, backBuffer);

                            // Create buffer for the resized copy
                            var width  = StreamWidth;
                            var height = StreamHeight;

                            using (var copy = new Texture2D(_d3dDevice, new Texture2DDescription {
                                Width = width, Height = height, MipLevels = 1, ArraySize = 1, Format = bitmap.Description.Format,
                                Usage = ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None
                            }))
                            {
                                // Copy region from captured bitmap to stream bitmap
                                _d3dDevice.ImmediateContext.CopySubresourceRegion(backBuffer, 0, ROI, copy, 0);

                                // access the copied data in a stream
                                _d3dDevice.ImmediateContext.MapSubresource(copy, 0, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream stream);
                                _buf.Add(new BufferedFrame(stream, new SizeInt32 {
                                    Width = width, Height = height
                                }, bitmap.Description.Format));
                                _d3dDevice.ImmediateContext.UnmapSubresource(copy, 0);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    needsReset     = true;
                    recreateDevice = true;
                }

                if (needsReset)
                {
                    _swapChain.ResizeBuffers(_swapChain.Description1.BufferCount, _lastSize.Width, _lastSize.Height, _swapChain.Description1.Format, _swapChain.Description1.Flags);
                    ResetFramePool(_lastSize, recreateDevice);
                }
            }
        }
Example #11
0
 void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
 {
     if (Active)
     {
         using (var frame = sender.TryGetNextFrame())
         {
             CanvasBitmap bitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice !, frame.Surface);
             _ = ShowBitmapOnTargetAsync(bitmap);
         }
     }
 }
Example #12
0
 private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
 {
     using (var frame = sender.TryGetNextFrame())
         using (var bitmap = WindowsCaptureHelper.CreateSharpDXTexture2D(frame.Surface))
         {
             _d3dDevice.ImmediateContext.CopyResource(bitmap, _screenTexture);
             var mapSource = _d3dDevice.ImmediateContext.MapSubresource(_screenTexture, 0, MapMode.Read, MapFlags.None);
             CopyFromMapSource(mapSource);
             _d3dDevice.ImmediateContext.UnmapSubresource(_screenTexture, 0);
         }
 }
Example #13
0
        private async void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            using (var frame = sender.TryGetNextFrame())
            {
                var sbmp = await CreateSoftwareBitmapFromSurface(frame.Surface);

                var frameFile =
                    await _outputFolder.CreateFileAsync("frame.jpg", CreationCollisionOption.GenerateUniqueName);

                SaveSoftwareBitmapToFile(sbmp, frameFile);
            }
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            using (var frame = sender.TryGetNextFrame())
            {
                using (var bitmap = CanvasBitmap.CreateFromDirect3D11Surface(_device, frame.Surface))
                    using (var drawingSession = _swapChain.CreateDrawingSession(Colors.Transparent))
                    {
                        drawingSession.DrawImage(bitmap);
                    }
            } // retire the frame

            _swapChain.Present();
        }
Example #15
0
        private async void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            var  scale  = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
            var  height = item.Size.Height - 32;
            bool result = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(new Size {
                Width = item.Size.Width / scale, Height = height / scale
            });

            if (!result)
            {
                bool result_full = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
            }

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
            await SaveRecordingAsync("a.mp4", 5000);
        }
        public void Read(SizeInt32 configSize, Func <IntPtr, int, int, int, int, object, bool> setPacket)
        {
            while (true)
            {
                if (!_continueProcessing)
                {
                    throw new OperationCanceledException();
                }


                if (configSize.Width != 0 &&
                    (configSize.Width != _initSize.Width ||
                     configSize.Height != _initSize.Height))
                {
                    Resize(configSize);
                }

                SizeInt32?newSize   = null;
                bool      processed = false;

                using (var frame = _framePool.TryGetNextFrame())
                {
                    if (frame != null)
                    {
                        if (frame.ContentSize.Width != _initSize.Width || frame.ContentSize.Height != _initSize.Height)
                        {
                            if (frame.ContentSize.Width != _requestedSize.Width || frame.ContentSize.Height != _requestedSize.Height)
                            {
                                _requestedSize = frame.ContentSize;
                                newSize        = frame.ContentSize;
                            }
                        }
                        processed = ProcessFrame(frame, setPacket);
                    }
                }

                if (newSize != null)
                {
                    _onSizeChanged(newSize.Value);
                }

                if (processed == true)
                {
                    break;
                }

                _frameAvailable.WaitOne();
            }
        }
Example #17
0
        private async void CpatureScreen_Click(object sender, RoutedEventArgs e)
        {
            Windows.Graphics.Capture.GraphicsCapturePicker graphicsCapturePicker = new Windows.Graphics.Capture.GraphicsCapturePicker();
            Windows.Graphics.Capture.GraphicsCaptureItem   graphicsCaptureItem   = await graphicsCapturePicker.PickSingleItemAsync();

            if (graphicsCaptureItem == null)
            {
                return;
            }
            CanvasDevice canvasDevice = new CanvasDevice();

            _franePool = Direct3D11CaptureFramePool.Create(canvasDevice, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, graphicsCaptureItem.Size);

            _franePool.FrameArrived += async(s, args) =>
            {
                using (var frame = _franePool.TryGetNextFrame())
                {
                    var canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, frame.Surface);

                    //await CpatureToImageFile(canvasBitmap);
                    CreateClipInMemory(canvasDevice, canvasBitmap);
                }
            };

            graphicsCaptureItem.Closed += (s, o) =>
            {
                CaptureStop_Click(null, null);
            };

            _session = _franePool.CreateCaptureSession(graphicsCaptureItem);

            timeSpans = new List <TimeSpan>();
            count     = 0;

            _session.StartCapture();

            QueryPerformanceCounter(out long qpc);
            QueryPerformanceFrequency(out long frq);
            performanceFrequency = frq;
            var milliseconds = 1000f * qpc / performanceFrequency;

            _timeSpan = TimeSpan.FromMilliseconds(milliseconds);

            CaptureAudio();

            btnCaptureStop.Visibility  = Visibility.Visible;
            btnCaptureStart.Visibility = Visibility.Collapsed;
        }
Example #18
0
        private void FramePool_FrameArrived(Direct3D11CaptureFramePool p, object o)
        {
            var frame = p.TryGetNextFrame();

            if (null == frame)
            {
                return;
            }

            if (screenCaptureQueue.Count > 120)
            {
                using (frame = screenCaptureQueue.Take()) { }
            }

            screenCaptureQueue.Add(frame);
        }
Example #19
0
        private void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // 下面参数暂时不能修改
            Direct3D11CaptureFramePool framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                                                           // 要在其中存储捕获的框架的缓冲区数量
                1,
                // 每个缓冲区大小
                item.Size); // Size of the buffers

            // 界面刷新的时候将会触发这个事件
            framePool.FrameArrived += async(s, a) =>
            {
                using (var frame = framePool.TryGetNextFrame())
                {
                    try
                    {
                        // 将获取到的 Direct3D11CaptureFrame 转 win2d 的
                        CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(
                            _canvasDevice,
                            frame.Surface);

                        CanvasComposition.Resize(_surface, canvasBitmap.Size);

                        using (var session = CanvasComposition.CreateDrawingSession(_surface))
                        {
                            session.Clear(Colors.Transparent);
                            session.DrawImage(canvasBitmap);
                        }
                    }
                    catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult))
                    {
                        // 设备丢失
                        await new MessageDialog("捕获应用窗口失败").ShowAsync();
                    }
                }
            };

            captureSession = framePool.CreateCaptureSession(item);
            captureSession.StartCapture();

            // 作为字段防止内存回收
            _direct3D11CaptureFramePool = framePool;
            _graphicsCaptureSession     = captureSession;
        }
Example #20
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            #region 帧池动态重构 暂存
            //var newSize = false;

            //using (var frame = sender.TryGetNextFrame())
            //{
            //    if (frame.ContentSize.Width != _lastSize.Width ||
            //        frame.ContentSize.Height != _lastSize.Height)
            //    {
            //        // 源已改变,故需要变换抓取大小,首先改变swap chain,之后是Texture
            //        newSize = true;
            //        _lastSize = frame.ContentSize;
            //        _swapChain.ResizeBuffers(
            //            2,
            //            _lastSize.Width,
            //            _lastSize.Height,
            //            SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //            SharpDX.DXGI.SwapChainFlags.None);
            //    }

            //    using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
            //    using (var backBuffer = _swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0))
            //    using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
            //    {
            //        _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
            //        _d3dDevice.ImmediateContext.CopyResource(sourceTexture, backBuffer);
            //    }

            //}

            //_swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);

            //if (newSize)//帧池重构
            //{
            //    _framePool.Recreate(
            //        _device,
            //        DirectXPixelFormat.B8G8R8A8UIntNormalized,
            //        2,
            //        _lastSize);
            //}
            #endregion

            _currentFrame = sender.TryGetNextFrame();
            _frameEvent.Set();
        }
Example #21
0
        private void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(
                this.measureCanvas.Device,                 // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    if (frame == null)
                    {
                        return;
                    }
                    ProcessFrame(frame);
                }

                StopCapture();// NOTE: just need one frame
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.IsCursorCaptureEnabled = false;
            _session.StartCapture();
        }
Example #22
0
        /// <summary>
        /// 帧到达事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using Direct3D11CaptureFrame frame = sender.TryGetNextFrame();

            if (frame == null)
            {
                return;
            }

            if (frame.ContentSize.Width != lastSize.Width || frame.ContentSize.Height != lastSize.Height)
            {
                // 我们捕捉到的东西变大了。
                // 我们需要先调整交换链的大小,然后blit像素。
                // 完成此操作后,请注销帧,然后重新创建帧池。
                newSize  = true;
                lastSize = frame.ContentSize;
                swapChain.ResizeBuffers(
                    2,
                    lastSize.Width,
                    lastSize.Height,
                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SharpDX.DXGI.SwapChainFlags.None);
            }

            using Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0);
            using Texture2D tex        = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface);
            d3dDevice.ImmediateContext.CopyResource(tex, backBuffer);
            // 保存当前帧到位图
            if (GetOneFrameFromBitmapEvent != null)
            {
                TryGetOneFrameToBitmap(tex);
            }
            // GetOneFrameToBitmap(tex);

            swapChain.Present(0, SharpDX.DXGI.PresentFlags.None);

            if (newSize)
            {
                framePool.Recreate(device, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, lastSize);
            }
        }
Example #23
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    // The thing we have been capturing has changed size.
                    // We need to resize our swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate our frame pool.
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.ResizeBuffers(
                        2,
                        _lastSize.Width,
                        _lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }

                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
                    using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                        using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
                        {
                            _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
                            _d3dDevice.ImmediateContext.CopyResource(sourceTexture, backBuffer);
                        }
            } // retire the frame

            _swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);

            if (newSize)
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
Example #24
0
        private async void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            //Need this to make more frames to arrive?
            if (parent != null)
            {
                counter++;
                if (counter > 1000000)
                {
                    counter = 0;
                }
                parent.msg("Arrived : " + counter.ToString());
            }

            if (isRecording == false)
            {
                return;
            }

            currentFrame = sender.TryGetNextFrame();
        }
Example #25
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();
            _item         = item;
            _lastSize     = _item.Size;
            _canvasDevice = new CanvasDevice();
            _framePool    = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers
            renderTarget = new CanvasRenderTarget(_canvasDevice, _item.Size.Width, _item.Size.Height, 96);
            foreach (var clip in ClipImages.Values)
            {
                clip.Start(_canvasDevice);
            }
            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    // 源已改变,故需要变换抓取大小,首先改变swap chain,之后是Texture
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.ResizeBuffers(
                        2,
                        _lastSize.Width,
                        _lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }

                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
                    using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                        using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
                        {
                            _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
                            _d3dDevice.ImmediateContext.CopyResource(sourceTexture, backBuffer);
                        }
            }

            _swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);

            if (newSize)//帧池重构
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                }
                using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                {
                    FrameArrived?.Invoke(new CapturedBitmap(bitmap));
                }
            }
            if (newSize)
            {
                _framePool.Recreate(_device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, _lastSize);
            }
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            lock ( m_lock )
            {
                var frame = sender.TryGetNextFrame();

                if (m_state != State.Started)
                {
                    frame?.Dispose();
                    return;
                }

                if (m_startRequest is not null)
                {
                    m_startRequest.SetActualStartPosition(frame.SystemRelativeTime);
                    m_startRequestCompletion !.Complete();

                    m_startRequest           = null;
                    m_startRequestCompletion = null;
                }

                if (m_sampleRequest is not null)
                {
                    m_frameAvailable       = false;
                    m_sampleRequest.Sample = MediaStreamSample.CreateFromDirect3D11Surface(frame.Surface, frame.SystemRelativeTime);
                    m_sampleRequestCompletion !.Complete();

                    m_sampleRequest           = null;
                    m_sampleRequestCompletion = null;
                }
                else
                {
                    m_frameAvailable = true;
                }

                m_currentFrame = frame;
            }
        }
Example #29
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(_canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, _item.Size);
            _framePool.FrameArrived += (s, a) =>
            {
                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
        }
Example #30
0
 private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
 {
     SetResult(sender.TryGetNextFrame());
 }