private void Execute(ISender client, GetDesktopResponse message) { lock (_syncLock) { if (!IsStarted) { return; } if (_codec == null || _codec.ImageQuality != message.Quality || _codec.Monitor != message.Monitor || _codec.Resolution != message.Resolution) { _codec?.Dispose(); _codec = new UnsafeStreamCodec(message.Quality, message.Monitor, message.Resolution); } using (MemoryStream ms = new MemoryStream(message.Image)) { // create deep copy & resize bitmap to local resolution OnReport(new Bitmap(_codec.DecodeData(ms), LocalResolution)); } message.Image = null; client.Send(new GetDesktop { Quality = message.Quality, DisplayIndex = message.Monitor }); } }
private unsafe void ProcessImage(byte[] data, int index) { if (!IsStreaming) { return; } lock (_unsafeStreamLock) { if (!IsStreaming) { return; } if (_unsafeStreamCodec != null && (_currentDevice != _webcamSettings.MonikerString || _currentResolution != _webcamSettings.Resolution)) { _unsafeStreamCodec.Dispose(); _unsafeStreamCodec = null; } if (_unsafeStreamCodec == null) { _currentResolution = _webcamSettings.Resolution; _currentDevice = _webcamSettings.MonikerString; _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None); } WriteableBitmap writeableBitmap; fixed(byte *dataPtr = data) writeableBitmap = _unsafeStreamCodec.DecodeData(dataPtr + index, (uint)(data.Length - index), Application.Current.Dispatcher); _framesReceived++; if (_writeableBitmap != writeableBitmap) { _writeableBitmap = writeableBitmap; RefreshWriteableBitmap?.Invoke(this, writeableBitmap); } } if (IsStreaming) { GetWebcamImage(); } if (FramesPerSecond == 0 && _framesReceived == 0) { _frameTimestamp = DateTime.UtcNow; } else if (DateTime.UtcNow - _frameTimestamp > TimeSpan.FromSeconds(1)) { FramesPerSecond = _framesReceived; _framesReceived = 0; _frameTimestamp = DateTime.UtcNow; } }
private void BeginAccept_Callback(IAsyncResult ar) { try { Socket sock = Server.EndAccept(ar); IUnsafeCodec decoder = new UnsafeStreamCodec(80); int FPS = 0; Stopwatch sw = Stopwatch.StartNew(); Stopwatch RenderSW = Stopwatch.StartNew(); while (sock.Connected) { //keep receiving data byte[] Header = ReceiveData(4, sock); if (Header.Length != 4) { break; } int length = BitConverter.ToInt32(Header, 0); byte[] Payload = ReceiveData(length, sock); if (Payload.Length != length) { break; } Bitmap decoded = decoder.DecodeData(new MemoryStream(Payload)); if (RenderSW.ElapsedMilliseconds >= (1000 / 20)) { this.pictureBox1.Image = (Bitmap)decoded.Clone(); RenderSW = Stopwatch.StartNew(); } FPS++; if (sw.ElapsedMilliseconds >= 1000) { this.Invoke(new Invoky(() => { this.Fpslbl.Text = "FPS: " + FPS; this.ScreenSizelbl.Text = "Screen Size: " + decoded.Width + " x " + decoded.Height; })); performanceChart1.AddValue(FpsLine, FPS); FPS = 0; sw = Stopwatch.StartNew(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } Server.BeginAccept(BeginAccept_Callback, null); }
//Convert byte array to image public Image ByteArrayToImage(byte[] ByteArrayIn) { using (var MS = new MemoryStream(ByteArrayIn)) { try { IUnsafeCodec UC = new UnsafeStreamCodec(60); return(UC.DecodeData(MS)); } catch { return(null); } } }
public unsafe void UpdateImage(byte[] data, int index, uint length) { if (_unsafeStreamCodec == null || _codecHeight != _height || _codecWidth != _width) { _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None); _codecHeight = _height; _codecWidth = _width; } fixed(byte *dataPtr = data) Image = _unsafeStreamCodec.DecodeData(dataPtr + index, length, Application.Current.Dispatcher); LastUpdateUtc = DateTime.UtcNow; }
private void BeginAccept_Callback(IAsyncResult ar) { try { Socket sock = Server.EndAccept(ar); IUnsafeCodec decoder = new UnsafeStreamCodec(80); int FPS = 0; Stopwatch sw = Stopwatch.StartNew(); Stopwatch RenderSW = Stopwatch.StartNew(); while (sock.Connected) { //keep receiving data byte[] Header = ReceiveData(4, sock); if (Header.Length != 4) { break; } int length = BitConverter.ToInt32(Header, 0); byte[] Payload = ReceiveData(length, sock); if (Payload.Length != length) { break; } Bitmap decoded = decoder.DecodeData(new MemoryStream(Payload)); if (RenderSW.ElapsedMilliseconds >= (1000 / 20)) { this.pictureBox1.Image = (Bitmap)decoded.Clone(); RenderSW = Stopwatch.StartNew(); } FPS++; } } catch (Exception ex) { MessageBox.Show(ex.Message); } Server.BeginAccept(BeginAccept_Callback, null); }