views:

4056

answers:

3
+5  A: 

The e.X and e.Y are relative to the picture box (e.g. if the mouse is in the upper left of the picture box, that's 0,0) .

The values for imagenMapa.Left and imagenMapa.Top are relative to the form (or whatever control contains imagenMapa)

If you try to mix values from these two systems without conversion, you're going to get jumps (like you're seeing).

You're probably better off converting the mouse position to the same coordinate system used by the thing that contains the picture box.

You could use imagenMapa.PointToScreen to get the mouse coordinates in screen coordinates (or Cursor.Position to get the position directly), and yourForm.PointToClient to get them back in the form coordinates.

Note that depending on your needs, you could accomplish "moving an image within a control" by overriding/handling the Paint event of a control and drawing the image yourself. If you did this, you could keep everything in the picturebox coordinates, since those are likely what you would use when you called graphicsObject.DrawImage.

Daniel LeCheminant
daniel this is nice theory but can you put it into code please? i have the same exact question but in VB.net. please help!
I__
+1  A: 

e.X & e.Y is in the coordinate space of the pictureBox, imagenMapa.Left & imagenMapa.Top is in the coordinate space of the Form. :-)

danbystrom
+1  A: 

Also don't forget to set your form to double buffered, that might help with the flickering, but for the actual positioning of it, I like Daniel L's suggestion

Fry
I'm using Compact Framework and Forms doesn't have double buffer. How can I do this?
VansFannel
This link describes how to create a "double buffered" Picture box. I'm sorry I guess I wasn't paying attn. when you said CF 2.0 http://stackoverflow.com/questions/574958/c-cf-winforms-and-double-buffer
Fry