move an image inside a picturebox Border

AMER SAID 396 Reputation points
2021-07-28T05:47:40.437+00:00

I used the following code to move the image inside the image box, but it may come out of the frame when crawling with the mouse right or left. I want the image not to leave the frame of the image.

my code The image is moving but is outside the frame designated for the image.

  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)  
        {  
            
                if (pictureBox1.Image != null)  
                {  
                    if ((e.Button == 0))  
                    {  
                          
  
                    }  
                    else  
                    {  
  
                        pictureBox1.Left = (pictureBox1.Left  
                              + (e.X - mousePosX));  
                        pictureBox1.Top = (pictureBox1.Top  
                                    + (e.Y - mousePosY));  
                   
                }  
  
                //}  
  
  
            }  
        }  

What I want is to move the image. And the picture does not come out of the picture box and it keeps moving inside the picture box frame and does not come out of it as shown in the picture..

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,927 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,421 Reputation points
    2021-07-28T11:31:52.103+00:00

    Try the following custom PictureBox

    using System;  
    using System.Drawing;  
    using System.Windows.Forms;  
      
    namespace PictureBoxCustomCore  
    {  
        public class ProPictureBox : PictureBox  
        {  
            private Point? _clickedPoint;  
            private ProTransformation _transformation;  
      
            public ProTransformation Transformation  
            {  
                set  
                {  
                    _transformation = FixTranslation(value);  
                    Invalidate();  
                }  
                get => _transformation;  
            }  
      
            public ProPictureBox()  
            {  
                _transformation = new ProTransformation(new Point(100, 0), .5f);  
                MouseDown += OnMouseDown;  
                MouseMove += OnMouseMove;  
                MouseUp += OnMouseUp;  
                MouseWheel += OnMouseWheel;  
                Resize += OnResize;  
            }  
      
            private ProTransformation FixTranslation(ProTransformation value)  
            {  
      
                var maxScale = Math.Max((double) Image.Width / ClientRectangle.Width, (double) Image.Height / ClientRectangle.Height);  
                if (value.Scale > maxScale)  
                {  
                    value = value.SetScale(maxScale);  
                }  
      
                if (value.Scale < 0.3)  
                {  
                    value = value.SetScale(0.3);  
                }  
      
                var rectSize = value.ConvertToIm(ClientRectangle.Size);  
                var max = new Size(Image.Width - rectSize.Width, Image.Height - rectSize.Height);  
      
                value = value.SetTranslate((new Point(Math.Min(value.Translation.X, max.Width), Math.Min(value.Translation.Y, max.Height))));  
                if (value.Translation.X < 0 || value.Translation.Y < 0)  
                {  
                    value = value.SetTranslate(new Point(Math.Max(value.Translation.X, 0), Math.Max(value.Translation.Y, 0)));  
                }  
      
                return value;  
            }  
      
            private void OnResize(object sender, EventArgs eventArgs)  
            {  
                if (Image == null)  
                {  
                    return;  
                }  
      
                Transformation = Transformation;  
            }  
      
            private void OnMouseWheel(object sender, MouseEventArgs e)  
            {  
                if (Image == null)  
                {  
                    return;  
                }  
      
                var transformation = _transformation;  
                var pos1 = transformation.ConvertToIm(e.Location);  
      
                transformation = e.Delta > 0 ? transformation.SetScale(Transformation.Scale / 1.25) : transformation.SetScale(Transformation.Scale * 1.25);  
      
                var pos2 = transformation.ConvertToIm(e.Location);  
                transformation = transformation.AddTranslate(pos1 - (Size) pos2);  
                Transformation = transformation;  
            }  
      
            private void OnMouseUp(object sender, MouseEventArgs mouseEventArgs)  
            {  
                _clickedPoint = null;  
            }  
      
            private void OnMouseMove(object sender, MouseEventArgs e)  
            {  
                if (_clickedPoint == null)  
                {  
                    return;  
                }  
      
                var p = _transformation.ConvertToIm((Size) e.Location);  
                Transformation = _transformation.SetTranslate(_clickedPoint.Value - p);  
            }  
      
            private void OnMouseDown(object sender, MouseEventArgs e)  
            {  
                if (Image == null)  
                {  
                    return;  
                }  
      
                Focus();  
      
                _clickedPoint = _transformation.ConvertToIm(e.Location);  
            }  
      
            protected override void OnPaint(PaintEventArgs e)  
            {  
                if (Image == null)  
                {  
                    return;  
                }  
      
                var imRect = Transformation.ConvertToIm(ClientRectangle);  
                e.Graphics.DrawImage(Image, ClientRectangle, imRect, GraphicsUnit.Pixel);  
            }  
      
            public void DecideInitialTransformation()  
            {  
                Transformation = new ProTransformation(Point.Empty, int.MaxValue);  
            }  
        }  
    }  
    

    118703-figure1.png

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 117.3K Reputation points
    2021-07-28T12:00:21.22+00:00

    Try something like this (inside the else block):

    Rectangle r = pictureBox1.Bounds;
    
    r.X = r.X + ( e.X - mousePosX );
    r.Y = r.Y + ( e.Y - mousePosY );
    
    if( r.Right > pictureBox1.Parent.ClientRectangle.Right )
        r.X = pictureBox1.Parent.ClientRectangle.Right - r.Width;
    
    if( r.Bottom > pictureBox1.Parent.ClientRectangle.Bottom )
        r.Y = pictureBox1.Parent.ClientRectangle.Bottom - r.Height;
    
    if( r.X < 0 )
        r.X = 0;
    
    if( r.Y < 0 )
        r.Y = 0;
    
    pictureBox1.Location = r.Location;
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.