public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client) { if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor) { StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor); } BitmapData bmpdata = null; try { LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Monitor); bmpdata = LastDesktopScreenshot.LockBits( new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite, LastDesktopScreenshot.PixelFormat); using (MemoryStream stream = new MemoryStream()) { StreamCodec.CodeImage(bmpdata.Scan0, new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), new Size(LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), LastDesktopScreenshot.PixelFormat, stream); new Packets.ClientPackets.GetDesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client); } } catch { new Packets.ClientPackets.GetDesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client); StreamCodec = null; } finally { if (LastDesktopScreenshot != null) { if (bmpdata != null) { LastDesktopScreenshot.UnlockBits(bmpdata); } LastDesktopScreenshot.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(); } } }
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(); } } }