Reputation: 3689
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:
Upvotes: 5
Views: 6262
Reputation: 51
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
Upvotes: 0
Reputation: 81620
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();
}
Upvotes: 2
Reputation: 62256
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.
Upvotes: 0
Reputation: 3527
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.
Upvotes: 0