示例#1
0
        private void OnMediaStreamSourceSampleRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
        {
            if (_isRecording)
            {
                using (var frame = GetNextFrame())
                    using (var lockSession = _multiThread.Lock())
                        using (var sourceTexture = Direct3D11Texture2D.CreateFromDirect3DSurface(frame.Surface))
                            using (var surface = _device.CreateTexture2D(new Direct3D11Texture2DDescription()
                            {
                                Base = sourceTexture.Description2D.Base,
                                MipLevels = sourceTexture.Description2D.MipLevels,
                                ArraySize = sourceTexture.Description2D.ArraySize,
                                Usage = Direct3DUsage.Default,
                                BindFlags = Direct3DBindings.ShaderResource,
                                CpuAccessFlags = 0,
                                MiscFlags = 0
                            }))
                            {
                                var timeStamp = frame.SystemRelativeTime;
                                _deviceContext.CopyResource(surface, frame.Surface);

                                try
                                {
                                    var sample = MediaStreamSample.CreateFromDirect3D11Surface(surface, timeStamp);
                                    args.Request.Sample = sample;
                                }
                                catch (Exception e)
                                {
                                    System.Diagnostics.Debug.WriteLine(e);
                                    args.Request.Sample = null;
                                }
                            }
            }
            else
            {
                args.Request.Sample = null;
            }
        }
        private async void ScreenshotButton_Click(object sender, RoutedEventArgs e)
        {
            var filePicker = new FileSavePicker();

            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.SuggestedFileName      = "screenshot";
            filePicker.DefaultFileExtension   = ".png";
            filePicker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
            var file = await filePicker.PickSaveFileAsync();

            if (file != null)
            {
                var capturePicker = new GraphicsCapturePicker();
                var item          = await capturePicker.PickSingleItemAsync();

                if (item != null)
                {
                    var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                        _device,
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        1,
                        item.Size);
                    var session = framePool.CreateCaptureSession(item);

                    var completionSource = new TaskCompletionSource <Direct3D11Texture2D>();
                    framePool.FrameArrived += (s, a) =>
                    {
                        using (var frame = s.TryGetNextFrame())
                        {
                            var frameTexture = Direct3D11Texture2D.CreateFromDirect3DSurface(frame.Surface);
                            var description  = frameTexture.Description2D;
                            description.Usage          = Direct3DUsage.Staging;
                            description.BindFlags      = 0;
                            description.CpuAccessFlags = Direct3D11CpuAccessFlag.AccessRead;
                            description.MiscFlags      = 0;
                            var copyTexture = _device.CreateTexture2D(description);

                            _deviceContext.CopyResource(copyTexture, frameTexture);

                            session.Dispose();
                            framePool.Dispose();

                            completionSource.SetResult(copyTexture);
                        }
                    };

                    session.StartCapture();
                    var texture = await completionSource.Task;
                    var bits    = texture.GetBytes();

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(
                            BitmapPixelFormat.Bgra8,
                            BitmapAlphaMode.Premultiplied,
                            (uint)item.Size.Width,
                            (uint)item.Size.Height,
                            1.0,
                            1.0,
                            bits);
                        await encoder.FlushAsync();
                    }

                    await Launcher.LaunchFileAsync(file);
                }
            }
        }