1

I need draw a rectangle only inside of picturebox with mousemove. without exceeding border the picturebox.

Dragging to the right or down works fine for me... How to make moviment reverse ?

My code below.

    Rectangle Rect = new Rectangle();
    private Point RectStartPoint;
    public Pen cropPen = new Pen(Color.Red, 2);

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        RectStartPoint = e.Location;
        picImagem.Invalidate();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point tempEndPoint = e.Location;
            Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));

            Rect = new Rectangle(
                Math.Min(tempEndPoint.X, Rect.Left),
                Math.Min(tempEndPoint.Y, Rect.Top),
                Math.Min(e.X - RectStartPoint.X, picImagem.ClientRectangle.Width - RectStartPoint.X),
                Math.Min(e.Y - RectStartPoint.Y, picImagem.ClientRectangle.Height - RectStartPoint.Y));

            picImagem.Refresh();
            picImagem.CreateGraphics().DrawRectangle(cropPen, Rect);

        }
    }
Pedro
  • 21
  • 3
  • Your exact question, as answered below, is an exact duplicate of the indicated question. You have other errors in your code, such as the failure to capture the mouse and using `CreateGraphics()` to draw into the control rather than handling the `Paint` event or drawing into a bitmap. The duplicate bug does show a correct method for drawing. If you have questions about your other errors, please ask a new question that specifically addresses those. – Peter Duniho Oct 07 '15 at 17:10
  • See also answers [here](https://stackoverflow.com/a/2529623/3538012) and [here](https://stackoverflow.com/a/6087367/3538012) for additional inspiration. – Peter Duniho Oct 07 '15 at 17:14

1 Answers1

0

You can correct your mouse move code this way:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Point tempEndPoint = e.Location;

        var point1 = new Point(
            Math.Max(0, Math.Min(RectStartPoint.X, tempEndPoint.X)),
            Math.Max(0, Math.Min(RectStartPoint.Y, tempEndPoint.Y)));

        var point2 = new Point(
            Math.Min(this.picImagem.Width, Math.Max(RectStartPoint.X, tempEndPoint.X)),
            Math.Min(this.picImagem.Height, Math.Max(RectStartPoint.Y, tempEndPoint.Y)));


        Rect.Location = point1;
        Rect.Size = new Size(point2.X - point1.X, point2.Y - point1.Y);


        picImagem.Refresh();
        picImagem.CreateGraphics().DrawRectangle(cropPen, Rect);

    }
}

In the above code we first normalize start and end of rectangle and make both start and end in bounds of rectangle. Then we draw it.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks man. Nice work :) – Pedro Oct 07 '15 at 16:54
  • 1
    While the above addresses your negative rectangle issue, I caution you that it doesn't address the bigger problems in your code: you fail to capture the mouse (so if dragging is interrupted by a change of window focus, the user can return to your code still dragging the rectangle, even without the mouse button down), and you are drawing to the control _in completely the wrong way_, using `CreateGraphics()` instead of invalidating the control and redrawing during a `Paint` event. As such, I do not consider it a very good answer; it's addressed only the most trivial part of your problem. – Peter Duniho Oct 07 '15 at 17:00
  • @PeterDuniho You are right, this code may need many things to do yet, but I only fixed the problem about **Draw Rectangle all directions** – Reza Aghaei Oct 07 '15 at 17:03