开发者

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.Inva开发者_开发技巧lidate();     
    }

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:

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

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:

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


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();     
}


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜