private void PictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var canvasInfo = this.GetCanvasInformation();
            var width      = 0.04;
            var height     = 0.06;

            var x = (e.X - canvasInfo.OffsetX + (width * canvasInfo.ScaledWidth / 2)) / canvasInfo.ScaledWidth;
            var y = (e.Y - canvasInfo.OffsetY + (height * canvasInfo.ScaledHeight / 2)) / canvasInfo.ScaledHeight;

            if (this._annotationImage.BoundingBoxes == null)
            {
                this._annotationImage.BoundingBoxes = new List <AnnotationBoundingBox>();
            }

            var newBoundingBox = new AnnotationBoundingBox
            {
                CenterX = (float)x,
                CenterY = (float)y,
                Height  = (float)height,
                Width   = (float)width
            };

            this._annotationImage.BoundingBoxes.Add(newBoundingBox);
            this._dragPoint = null;
            this.ImageEdited?.Invoke(this._annotationImage);
        }
Пример #2
0
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            this._selectedItem = null;

            this._mousePosition = new Point(0, 0);
            this.pictureBox1.Invalidate();
        }
        private void CreateBoundingBox(PointF point1, PointF point2)
        {
            var canvasInfo = this.GetCanvasInformation();

            var topLeftCorner     = new PointF(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
            var bottomRightCorner = new PointF(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));

            topLeftCorner     = this.ClampPoint(topLeftCorner);
            bottomRightCorner = this.ClampPoint(bottomRightCorner);

            var width  = (bottomRightCorner.X - topLeftCorner.X) / canvasInfo.ScaledWidth;
            var height = (bottomRightCorner.Y - topLeftCorner.Y) / canvasInfo.ScaledHeight;

            var x = (topLeftCorner.X - canvasInfo.OffsetX + (width * canvasInfo.ScaledWidth / 2)) / canvasInfo.ScaledWidth;
            var y = (topLeftCorner.Y - canvasInfo.OffsetY + (height * canvasInfo.ScaledHeight / 2)) / canvasInfo.ScaledHeight;

            if (this._annotationImage.BoundingBoxes == null)
            {
                this._annotationImage.BoundingBoxes = new List <AnnotationBoundingBox>();
            }

            var newBoundingBox = new AnnotationBoundingBox
            {
                CenterX = (float)x,
                CenterY = (float)y,
                Width   = (float)width,
                Height  = (float)height
            };

            this._selectedBoundingBox = newBoundingBox;

            this._annotationImage.BoundingBoxes.Add(newBoundingBox);
            this._dragPoint = null;
            this.ImageEdited?.Invoke(this._annotationImage);
        }
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this._createBoundingBox)
            {
                this._createBoundingBox = false;
                if (this._canPlaceBoundingBox)
                {
                    this.CreateBoundingBox(this._creationPoint, e.Location);
                }

                return;
            }

            if (this._dragPoint?.Type == DragPointType.Delete)
            {
                this._annotationImage?.BoundingBoxes?.Remove(this._draggedBoundingBox);
            }

            this._draggedBoundingBox = null;

            this._mousePosition = new Point(0, 0);
            this.rotatingPictureBox.Invalidate();

            this.ImageEdited?.Invoke(this._annotationImage);
        }
Пример #5
0
 public AnnotationBoundingBox(AnnotationBoundingBox boundingBox)
 {
     this.ObjectIndex = boundingBox.ObjectIndex;
     this.CenterX     = boundingBox.CenterX;
     this.CenterY     = boundingBox.CenterY;
     this.Width       = boundingBox.Width;
     this.Height      = boundingBox.Height;
 }
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this._annotationImage?.BoundingBoxes == null)
            {
                return;
            }

            var drawOffset = this._mouseDragElementSize / 2;
            var canvasInfo = this.GetCanvasInformation();

            var boundingBoxes = this._annotationImage?.BoundingBoxes;

            if (boundingBoxes == null)
            {
                return;
            }

            foreach (var boundingBox in boundingBoxes)
            {
                var width  = boundingBox.Width * canvasInfo.ScaledWidth;
                var height = (boundingBox.Height * canvasInfo.ScaledHeight);
                var x      = (boundingBox.CenterX * canvasInfo.ScaledWidth) - (width / 2) + canvasInfo.OffsetX;
                var y      = (boundingBox.CenterY * canvasInfo.ScaledHeight) - (height / 2) + canvasInfo.OffsetY;

                var rectangle = new Rectangle((int)x, (int)y, (int)width, (int)height);

                var startDrag = false;

                var dragPoints = this.GetDragPoints(rectangle, drawOffset);
                foreach (var dragPoint in dragPoints)
                {
                    if (this.PointDistance(this._mousePosition, new Point(dragPoint.Point.X, dragPoint.Point.Y)) < 15)
                    {
                        this._dragPoint = dragPoint;
                        startDrag       = true;
                        break;
                    }
                }

                if (startDrag)
                {
                    this._selectedBoundingBox = boundingBox;
                    break;
                }
                else
                {
                    this._dragPoint           = null;
                    this._selectedBoundingBox = null;
                }
            }

            this._mousePosition = e.Location;
            this.pictureBox1.Invalidate();
        }
        private Rectangle GetRectangle(AnnotationBoundingBox boundingBox)
        {
            var canvasInfo = this.GetCanvasInformation();

            var width  = boundingBox.Width * canvasInfo.ScaledWidth;
            var height = (boundingBox.Height * canvasInfo.ScaledHeight);
            var x      = (boundingBox.CenterX * canvasInfo.ScaledWidth) - (width / 2) + canvasInfo.OffsetX;
            var y      = (boundingBox.CenterY * canvasInfo.ScaledHeight) - (height / 2) + canvasInfo.OffsetY;

            return(new Rectangle((int)x, (int)y, (int)width, (int)height));
        }
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this._dragPoint?.Type == DragPointType.Delete)
            {
                this._annotationImage?.BoundingBoxes.Remove(this._selectedBoundingBox);
            }

            this._selectedBoundingBox = null;

            this._mousePosition = new Point(0, 0);
            this.pictureBox1.Invalidate();

            this.ImageEdited?.Invoke(this._annotationImage);
        }
        public void OnKeyDown(object sender, KeyEventArgs e)
        {
            AnnotationBoundingBox currentBoundingBox = null;

            var boundingBoxes = this._annotationImage?.BoundingBoxes;

            if (boundingBoxes != null)
            {
                foreach (var boundingBox in boundingBoxes)
                {
                    var rectangle = this.GetRectangle(boundingBox);

                    var biggerRectangle = Rectangle.Inflate(rectangle, 20, 20);
                    if (biggerRectangle.Contains(this._mousePosition))
                    {
                        currentBoundingBox = boundingBox;
                        break;
                    }
                }
            }

            if (currentBoundingBox == null)
            {
                return;
            }

            var index = -1;

            if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
            {
                index = (int)e.KeyCode - (int)Keys.D0;
            }
            else if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
            {
                index = (int)e.KeyCode - (int)Keys.NumPad0;
            }

            if (index == -1 || index >= this._objectClasses.Count)
            {
                return;
            }

            currentBoundingBox.ObjectIndex = index;
            this.pictureBox1.Invalidate();

            this.ImageEdited?.Invoke(this._annotationImage);
        }
        private void MoveOrResize(
            AnnotationBoundingBox boundingBox,
            PointF translation,
            CanvasInfo canvasInfo,
            KeyboardOperation keyboardOperation,
            ScaleOperation scaleOperation)
        {
            if (keyboardOperation == KeyboardOperation.Move)
            {
                boundingBox.CenterX += translation.X / (float)canvasInfo.ScaledWidth;
                boundingBox.CenterY += translation.Y / (float)canvasInfo.ScaledWidth;

                boundingBox.CenterX = boundingBox.CenterX.Clamp(boundingBox.Width / 2, 1 - boundingBox.Width / 2);
                boundingBox.CenterY = boundingBox.CenterY.Clamp(boundingBox.Height / 2, 1 - boundingBox.Height / 2);
            }
            else
            {
                var inverseFac = (scaleOperation == ScaleOperation.Inverse ? -1 : 1);

                var newCenterX = boundingBox.CenterX + translation.X / (float)canvasInfo.ScaledWidth / 2;
                var newWidth   = boundingBox.Width + translation.X / (float)canvasInfo.ScaledWidth * inverseFac;

                if (newCenterX - newWidth / 2 >= 0 && newCenterX + newWidth / 2 <= 1 && newWidth * canvasInfo.ScaledWidth > this._minSize.Width)
                {
                    boundingBox.CenterX = newCenterX;
                    boundingBox.Width   = newWidth;
                }

                var newCenterY = boundingBox.CenterY + translation.Y / (float)canvasInfo.ScaledHeight / 2;
                var newHeight  = boundingBox.Height + translation.Y / (float)canvasInfo.ScaledHeight * inverseFac;

                if (newCenterY - newHeight / 2 >= 0 && newCenterY + newHeight / 2 <= 1 && newHeight * canvasInfo.ScaledHeight > this._minSize.Height)
                {
                    boundingBox.CenterY += translation.Y / (float)canvasInfo.ScaledHeight / 2;
                    boundingBox.Height  += translation.Y / (float)canvasInfo.ScaledHeight * inverseFac;
                }
            }
        }
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            var startDrag = false;

            this._createBoundingBox = false;

            var boundingBoxes = this._annotationImage?.BoundingBoxes;

            if (boundingBoxes != null)
            {
                var drawOffset = this._mouseDragElementSize / 2;
                var canvasInfo = this.GetCanvasInformation();

                var orderedBoundingBoxes = this.OrderBoundingBoxes(boundingBoxes);

                foreach (var boundingBox in orderedBoundingBoxes)
                {
                    this._draggedBoundingBox = null;
                    this._dragPoint          = null;

                    var width  = boundingBox.Width * canvasInfo.ScaledWidth;
                    var height = (boundingBox.Height * canvasInfo.ScaledHeight);
                    var x      = (boundingBox.CenterX * canvasInfo.ScaledWidth) - (width / 2) + canvasInfo.OffsetX;
                    var y      = (boundingBox.CenterY * canvasInfo.ScaledHeight) - (height / 2) + canvasInfo.OffsetY;

                    var rectangle = new Rectangle((int)x, (int)y, (int)width, (int)height);

                    var biggerRectangle = Rectangle.Inflate(rectangle, 20, 20);
                    if (biggerRectangle.Contains(this._mousePosition))
                    {
                        startDrag = true;

                        this._grabOffsetX = (this._mousePosition.X - rectangle.X) / canvasInfo.ScaledWidth;
                        this._grabOffsetY = (this._mousePosition.Y - rectangle.Y) / canvasInfo.ScaledHeight;
                    }

                    var dragPoints = this.GetDragPoints(rectangle, drawOffset);
                    foreach (var dragPoint in dragPoints)
                    {
                        if (this.PointDistance(this._mousePosition, new Point(dragPoint.Point.X, dragPoint.Point.Y)) < this._maxMouseDistanceToDragPoint)
                        {
                            this._dragPoint = dragPoint;

                            startDrag = true;

                            this._grabOffsetX = (this._dragPoint.Point.X - rectangle.X) / canvasInfo.ScaledWidth;
                            this._grabOffsetY = (this._dragPoint.Point.Y - rectangle.Y) / canvasInfo.ScaledHeight;

                            this._mouseOffsetX = this._dragPoint.Point.X - this._mousePosition.X;
                            this._mouseOffsetY = this._dragPoint.Point.Y - this._mousePosition.Y;

                            break;
                        }
                    }

                    if (startDrag)
                    {
                        this._selectedBoundingBox = boundingBox;
                        this._draggedBoundingBox  = boundingBox;
                        this._selectedObjectRect  = new RectangleF(rectangle.X / (float)canvasInfo.ScaledWidth, rectangle.Y / (float)canvasInfo.ScaledHeight,
                                                                   rectangle.Width / (float)canvasInfo.ScaledWidth, rectangle.Height / (float)canvasInfo.ScaledHeight);

                        break;
                    }
                }
            }

            if (!startDrag)
            {
                this._createBoundingBox = true;
                this._creationPoint     = e.Location;
            }

            this._mousePosition = e.Location;
            this.rotatingPictureBox.Invalidate();
        }