0

I have loaded the image of a car into the picturebox like this.

   private void btn_LoadPattern_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "JPEG Files|*.jpg";

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.ImageLocation = openFileDialog.FileName;
        }
    }

I would like to draw a rectangle over this image and add arrow buttons on the same form, so user will be able to move the rectangle to indicate where on the picture the registration plate is located.

The problem is that if I start drawing rectangle it is covered by the car picture. The other question is how to redraw the rectangle in the new position while the user clieck move right/left/top/down button.

Anyhelp will be much appreciated.

dzwonek
  • 1
  • 1
  • 1

1 Answers1

2

In this case, I wouldn't use PictureLocation, I would do this instead:

pictureBox1.BackgroundImage = Image.FromFile(openFileDialog.FileName);

Now if you draw a rectangle in the PictureBox it should get drawn over the car image.

Secondly, use the Paint event of PictureBox.
Something like this should do the trick.

Rectangle MyRectangle;
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Create a local version of the graphics object for the PictureBox.
    Graphics g = e.Graphics;

    g.DrawRectangle(Pens.Black, MyRectangle);
}

And you should be able to handle keystrokes to modify MyRectangle according to the arrow keys, then call pictureBox1.Invalidate() to have the Paint event redraw the rectangle at the new location.

BeemerGuy
  • 8,139
  • 2
  • 35
  • 46