protected override void OnMouseUp(MouseEventArgs e) { try { if (rectSelection.Width <= 0 || rectSelection.Height <= 0) { OnCancel(new EventArgs()); return; } foreach (var form in forms) { form.Opacity = 0; } var point = PointToScreen(rectSelection.Location); if (rectSelection.Width < MIN_CAPTURE_WIDTH || rectSelection.Height < MIN_CAPTURE_HEIGHT) { UIUtilities.ShowMessageDialog("Capture region is too small.", "Invalid Capture Size"); } else if (rectSelection.Width > MAX_CAPTURE_WIDTH || rectSelection.Height > MAX_CAPTURE_HEIGHT) { UIUtilities.ShowMessageDialog("Capture region is too large.", "Invalid Capture Size"); } else { Bitmap = ScreenHelper.CaptureScreen(point, rectSelection.Width, rectSelection.Height); OnAreaSelected(new EventArgs()); } } finally { CloseForms(); } }
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 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 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(); } } }