private void screenPreview_Paint(object sender, PaintEventArgs e) { if (_selectedScreen == null) { return; } string screenName = _selectedScreen.DeviceName; var region = new ScreenRegion(screenName, new RectangleFSerializable(0, 0, 1, 1)); CaptureResult captureResult = _captureService.CaptureScreenRegions(new List <ScreenRegion> { region }, 0)[0]; Bitmap imageToShow; if (captureResult.State == CaptureState.NoChanges) { if (_lastBitmap == null) { return; } imageToShow = _lastBitmap; } else if (captureResult.State == CaptureState.NewBitmap) { if (_lastBitmap != null) { _lastBitmap.Dispose(); } imageToShow = captureResult.Bitmap; _lastBitmap = imageToShow; } else { throw new InvalidOperationException("Invalid capture state"); } e.Graphics.DrawImage(imageToShow, 0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height); var selectedRegionOutput = screenRegionsList.SelectedItem as ScreenRegionOutput; foreach (ScreenRegionOutput screenRegionOutput in screenRegionsList.Items.Cast <ScreenRegionOutput>().Where(x => x.ScreenRegion.ScreenName == screenName)) { var isSelected = selectedRegionOutput == screenRegionOutput; RectangleFSerializable screenRegionRect = screenRegionOutput.ScreenRegion.Rectangle; e.Graphics.DrawRectangle(isSelected ? Pens.Lime : Pens.Red, screenRegionRect.X * e.ClipRectangle.Width, screenRegionRect.Y * e.ClipRectangle.Height, screenRegionRect.Width * e.ClipRectangle.Width, screenRegionRect.Height * e.ClipRectangle.Height); } }
private static void SetRegion(ref RectangleFSerializable rectangle, PointF?topLeft, PointF?bottomRight) { PointF topLeftValue = topLeft ?? rectangle.Location; PointF bottomRightValue = bottomRight ?? PointF.Add(rectangle.Location, rectangle.Size); if (topLeftValue.X > bottomRightValue.X) { bottomRightValue.X = topLeftValue.X; } if (topLeftValue.Y > bottomRightValue.Y) { bottomRightValue.Y = topLeftValue.Y; } rectangle.X = topLeftValue.X; rectangle.Y = topLeftValue.Y; rectangle.Width = bottomRightValue.X - topLeftValue.X; rectangle.Height = bottomRightValue.Y - topLeftValue.Y; }
private void screenPreview_Click(object sender, EventArgs e) { var mouseArgs = e as MouseEventArgs; if (mouseArgs == null || mouseArgs.Button != MouseButtons.Left) { return; } float posX = (float)mouseArgs.X / screenPreview.Width; float posY = (float)mouseArgs.Y / screenPreview.Height; var region = screenRegionsList.SelectedItem as ScreenRegionOutput; if (region != null) { RectangleFSerializable rect = region.ScreenRegion.Rectangle; switch (_screenClickMode) { case ScreenClickMode.None: return; case ScreenClickMode.SetTopLeft: SetRegion(ref rect, new PointF(posX, posY), null); break; case ScreenClickMode.SetBottomRight: SetRegion(ref rect, null, new PointF(posX, posY)); break; default: throw new ArgumentOutOfRangeException(); } region.ScreenRegion = new ScreenRegion(_selectedScreen.DeviceName, rect); } UpdateSelectedItem(screenRegionsList); SetScreenClickMode(ScreenClickMode.None); }
public ScreenRegion(string screenName, RectangleFSerializable rectangle) { ScreenName = screenName; Rectangle = rectangle; }