Esempio n. 1
0
        private void _imageViewer_MouseMove(object sender, MouseEventArgs e)
        {
            RasterImage image = _imageViewer.Image;

            if (image == null)
            {
                return;
            }

            // Here is the point in viewer (physical or client) coordinates
            // This point is the mouse position
            LeadPoint viewerPoint = LeadPoint.Create(e.X, e.Y);

            // Convert to image coordinates (top-left view perspective, since we are
            // only interested in the zoom/scroll values and not trying to get to a
            // certain pixel in the image data)
            LeadPoint imagePoint = _imageViewer.ConvertPoint(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, viewerPoint);

            ShowMousePositionAndImageColor(image, viewerPoint, imagePoint);

            if (_isMovingOverlayRect)
            {
                // We are moving the overlay rect

                // After we are done with moving the overlay rect to a new position,
                // we need to re-paint to show the effect. To optimize the code, we will
                // only repaint the combination of the old and new rects

                // Get the current (soon to be old) overlay rect in viewer coordinates
                LeadRect oldImageRect  = LeadRect.Create(_overlayRect.X, _overlayRect.Y, _overlayRect.Width, _overlayRect.Height);
                LeadRect oldViewerRect = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, oldImageRect);

                // Get the difference
                int dx = e.X - _lastMovePoint.X;
                int dy = e.Y - _lastMovePoint.Y;

                if (dx != 0 || dy != 0)
                {
                    // Has moved

                    LeadRect newViewerRect = oldViewerRect;
                    newViewerRect.Offset(dx, dy);

                    // Now re-calculate new overlay rectangle in image coordinates
                    LeadRect newImageRect = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, newViewerRect);

                    // Make sure the image rectangle does not go outside the image boundaries
                    if (newImageRect.X < 0)
                    {
                        newImageRect.Offset(-newImageRect.X, 0);
                    }
                    if (newImageRect.Y < 0)
                    {
                        newImageRect.Offset(0, -newImageRect.Y);
                    }
                    if ((newImageRect.X + newImageRect.Width) > image.ImageWidth)
                    {
                        newImageRect.Offset(-(newImageRect.X + newImageRect.Width - image.ImageWidth), 0);
                    }
                    if ((newImageRect.Y + newImageRect.Height) > image.ImageHeight)
                    {
                        newImageRect.Offset(0, -(newImageRect.Y + newImageRect.Height - image.ImageHeight));
                    }

                    // Check if it has really changed
                    if (newImageRect.X != oldImageRect.X ||
                        newImageRect.Y != oldImageRect.Y ||
                        newImageRect.Width != oldImageRect.Width ||
                        newImageRect.Height != oldImageRect.Height)
                    {
                        // Yes
                        _overlayRect = new LeadRect(newImageRect.X, newImageRect.Y, newImageRect.Width, newImageRect.Height);

                        _imageViewer.Invalidate();
                    }

                    // Save the new moving position
                    _lastMovePoint = LeadPoint.Create(e.X, e.Y);
                }
            }
        }