Esempio n. 1
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            _dispatcherTimer = new DispatcherTimer {
                Interval = TimeSpan.FromSeconds(1)
            };
            _dispatcherTimer.Tick += DispatcherTimerOnTick;
            _dispatcherTimer.Start();

            new Thread(() =>
            {
                using (var streamCodec = new UnsafeStreamCodec(new JpgCompression(70),
                                                               UnsafeStreamCodecParameters.None))
                    using (var decoderCodec = new UnsafeStreamCodec(new JpgCompression(70),
                                                                    UnsafeStreamCodecParameters.None))
                        using (var cursorCodec = new CursorStreamCodec())
                            using (var decodeCursorCodec = new CursorStreamCodec())
                                using (var screenService = new FrontBufferService())
                                {
                                    streamCodec.ImageQuality = 70;
                                    screenService.Initialize(0);
                                    WriteableBitmap currentWriteableBitmap = null;

                                    while (!_isClosed)
                                    {
                                        var sw   = Stopwatch.StartNew();
                                        var data = screenService.CaptureScreen(streamCodec, cursorCodec, true);
                                        if (data == null)
                                        {
                                            continue;
                                        }
                                        //Debug.Print($"Screen captured ({sw.ElapsedMilliseconds})");

                                        unsafe
                                        {
                                            byte[] bytes;
                                            using (var ms = new MemoryStream())
                                            {
                                                data.WriteIntoStream(ms);
                                                bytes = ms.ToArray();
                                            }

                                            var cursorData = cursorCodec.CodeCursor();

                                            fixed(byte *bytePtr = bytes)
                                            {
                                                sw.Reset();
                                                //Debug.Print("Decode " + bytes.Length);
                                                var newBitmap =
                                                    decoderCodec.AppendModifier(decodeCursorCodec.CreateModifierTask(cursorData, 0,
                                                                                                                     cursorData.Length)).DecodeData(bytePtr, (uint)bytes.Length, Dispatcher);
                                                //Debug.Print($"Screen decoded ({sw.ElapsedMilliseconds})");
                                                if (newBitmap != currentWriteableBitmap)
                                                {
                                                    currentWriteableBitmap = newBitmap;
                                                    Dispatcher.Invoke(() => ImageAsd.Source = currentWriteableBitmap);
                                                }
                                                Interlocked.Increment(ref _currentFps);
                                            }
                                        }
                                    }
                                }
            }).Start();
        }