Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the real Location of mouse in actual size of image, not the location of mouse in PictureBox

I have a image with size of 1278 X 958, I am doing some image processing on that.

As the image size is big,I load it with following command in a pictureBox:

imGrayShow = cap.QueryGrayFrame();
picBOriginal.Image = imGrayShow.ToBitmap();

pictureBox size is 299 X 204 and to see my original image in pictureBox I put the

pictureBox Size Mode= StretchImage

The story starts here:

I want to use mouse-down & mouse-up event to get the location of the area that the user selects with following command:

 private void picBOriginal_MouseUp(object sender, MouseEventArgs e)
        {
            if (stopEventMouseDrag == true)
            {
                endP = e.Location;

                if (startP != endP)
                {
                    stopEventMouseDrag = false;
                }
            }
        }

        private void picBOriginal_MouseDown(object sender, MouseEventArgs e)
        {
            if (stopEventMouseDrag == true)
            {
                startP = e.Location;
            }
        }

What I get as the startP and endP is (145,2) and (295,83) respectively; but these are the location of mouse in pictureBox, whereas I am expecting to find out the real location of mouse down and mouse down in my original Image. ( which are=> startP: 890,1 endP: 1277,879 )

how could I possibly get the REAL location of startP-endP in the original Image?

like image 689
farzin parsa Avatar asked Oct 28 '25 03:10

farzin parsa


2 Answers

I think your math is a bit off. If you have a 1278 x 958 image and you want to scale it down to 299 pixels wide, then you need to divide everything by 1278 / 299 which is 4.27. To keep the aspect ratio the same, the width would need to be 958 / 4.27 which rounds to 224.

Then when you receive the coordinates from the mouse down & up events, you just multiply the coordinates by 4.27 to scale up the values to the original image.

like image 163
David Avatar answered Oct 29 '25 17:10

David


startP and endP are point relative to your picture box, or relative to the screen? From what I remember, the Location in a MouseEventArgs is relative to the form, which isn't very useful.

So... It would probably end up something like this:

// Position of the mouse, relative to the upper left corner of the picture box.
Point controlRelative = myPictureBox.PointToClient(MousePosition);
// Size of the image inside the picture box
Size imageSize = myPictureBox.Image.Size;
// Size of the picture box
Size boxSize = myPictureBox.Size;

Point imagePosition = new Point((imageSize.Width / boxSize.Width) * controlRelative.X,
                                (imageSize.Height / boxSize.Height) * controlRelative.Y);
like image 22
LightStriker Avatar answered Oct 29 '25 19:10

LightStriker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!