简体   繁体   中英

Drawing rectangle on picturebox - how to limit area of rectangle?

i'm drawing rectangle on picturebox with mouse events:

private void StreamingWindow_MouseDown(object sender, MouseEventArgs e)
    {
              rect = new Rectangle(e.X, e.Y, 0, 0);
              this.Invalidate();       
    }

    private void StreamingWindow_Paint(object sender, PaintEventArgs e)
    {

       if (painting == true)
        {

            using (Pen pen = new Pen(Color.Red, 2))
            {
                e.Graphics.DrawRectangle(pen, rect);
            }
        }
    }

    private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
    {       
           if (e.Button == MouseButtons.Left)
           {
               // Draws the rectangle as the mouse moves
               rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
           }
           this.Invalidate();     
    }

After drawing rectangle i can capture inside of it, and save as jpg.

What's my problem?

I can draw retangle which borders are outside area of picturebox:

在此输入图像描述

How can i limit area of rectangle that border of picturebox is max allowed location of rectangle?

Sorry for my english, i hope you'll understand my problem :) So as a result i'd like to have something like this:

在此输入图像描述

private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
{       
  if (e.Button == MouseButtons.Left)
  {
    // Draws the rectangle as the mouse moves
    rect = new Rectangle(rect.Left, rect.Top, Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left), Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
  }
  this.Invalidate();     
}

Another way for solving this is preventing rectangle to be drown outside of pictureBox control

private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (e.X < StreamingWindow.Width && Math.Abs(e.Y) < StreamingWindow.Height)
                // Draws the rectangle as the mouse moves
                rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y -rect.Top);
        }
        this.Invalidate();
    }

Somebody can find this solution more useful

I would say the easiest way to achieve this, and I personally think more natural one from UX perspective, is: after MouseUp check if BottomLeft corner of rectangle is outside of area of picture box, if so, just bring it "back" and align it to the picture box's angle just as you drawed .

EDIT

Just to give you an idea what I'm talking about, a pseudocode

    private void StreamingWindow_MouseUp(object sender, MouseEventArgs e)
    {
              if(rect.Right > myPictureBox.ClientRectangle.Right)
              {
                 rect.Width = myPictureBox.Right - rect.Left - someoffset;                     
              }       
              if(rect.Bottom > myPictureBox.ClientRectangle.Bottom)
              {
                 rect.Height= myPictureBox.Bottom - rect.Top - someoffset;                     
              }       
    }

Something like this. But you need to check this.

Why not setting the rect coords to something

 rect = new Rectangle(min(e.X, pictureBoxX), min(e.Y, pictureBoxY), 0, 0);

You need to calculate pictureX and pictureY according to the location and position of the picture box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM