I have an application that will load an image into a pictureBox and will give me the value of RGB where the cross cursor is located, I can move the cursor into mutiple direction in picturebox space. Instead I want to move into image pixel space, so if I zoomed in or out I want to cursor jump from pixel to another.
any Ideas, do I need to get PixelHeight and PixelWidth of the image everytime i zoom and is Stride is how to calculate how many pixels there is?
Changing the Cursor.Position
is simple but will interfer with actually moving the mouse.
Instead you can try this:
Here is an example:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Point mouse = pictureBox1.PointToClient(Cursor.Position);
float zoom = 1f * pictureBox1.ClientRectangle.Width / pictureBox1.Image.Width;
int pixelWidth = (int)Math.Round(zoom);
int x = mouse.X / pixelWidth * pixelWidth;
int y = mouse.Y / pixelWidth * pixelWidth;
if (pictureBox1.ClientRectangle.Contains(mouse))
{
e.Graphics.DrawLine(Pens.White, x, y - 4, x, y + 4);
e.Graphics.DrawLine(Pens.White, x - 4, y, x + 4, y);
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Cursor.Hide();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Cursor.Show();
pictureBox1.Invalidate(); // clear the last one!
}
Of course you need to calculate the pixel width only when loading or sizing the iamge. I kept it in place for clarity.