Reputation: 213
I am new with c#. I have a image on picturebox, i would like to draw a rectangle on the image to capture X,Y coordinate and width/height of the rectangle that i will draw (against the image on picturebox). i know i must do something on pictureBox1_MouseEnter..etc. but i don't know where to start.
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle Rect = RectangleToScreen(this.ClientRectangle);
}
I would appreciated if any one can give me sample code.
Thanks.
Upvotes: 2
Views: 9930
Reputation: 244692
You don't want to use MouseEnter
at all. You want to use MouseDown
, because you don't want to start tracking the rectangle the user draws until after they click the mouse button.
So in the MouseDown
event handler method, save the current coordinates of the cursor. This is the starting position of the rectangle, point (X, Y).
private Point initialMousePos;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.initialMousePos = e.Location;
}
And then, in the MouseUp
event, which is raised when the user stops dragging and releases the mouse button, you want to save the final ending point of the mouse cursor, and combine that with the initial starting point to build a rectangle. The easiest way to build such a rectangle is using the FromLTRB
static method of the Rectangle
class:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// Save the final position of the mouse
Point finalMousePos = e.Location;
// Create the rectangle from the two points
Rectangle drawnRect = Rectangle.FromLTRB(
this.initialMousePos.X,
this.initialMousePos.Y,
finalMousePos.X,
finalMousePos.Y);
// Do whatever you want with the rectangle here
// ...
}
You probably want to combine this with some painting code in the MouseMove
event handler method to give the user a visual indication of the rectangle that they're drawing while they're drawing it.
The right way to do this is to handle the MouseMove
event to get the current position of the mouse, and then call the Invalidate
method, which will raise the Paint
event. All of your painting code should be in the Paint
event handler. This is a better approach than the one taken by a lot of samples you find on the web, where you'll see something like CreateGraphics
called inside of the MouseMove
event handler method. Avoid that, if possible.
Sample implementation:
private Point currentMousePos;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Save the current position of the mouse
currentMousePos = e.Location;
// Force the picture box to be repainted
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// Create a pen object that we'll use to draw
// (change these parameters to make it any color and size you want)
using (Pen p = new Pen(Color.Blue, 3.0F))
{
// Create a rectangle with the initial cursor location as the upper-left
// point, and the current cursor location as the bottom-right point
Rectangle currentRect = Rectangle.FromLTRB(
this.initialMousePos.X,
this.initialMousePos.Y,
currentMousePos.X,
currentMousePos.Y);
// Draw the rectangle
e.Graphics.DrawRectangle(p, currentRect);
}
}
A couple of things to note about this code if you've never done any type of drawing before. The first thing is that I've wrapped the creation of a Pen
object (which we use to do the actual drawing) in a using
statement. This is good general practice any time you create an object that implements the IDisposable
interface, such as brushes and pens, to prevent a memory leak in your application.
Also, the PaintEventArgs
provide us with an instance of the Graphics
class, which encapsulates all of the basic drawing functionality in a .NET application. All you have to do is call methods like DrawRectangle
or DrawImage
on that class instance, passing in the appropriate objects as parameters, and all the drawing gets done for you automatically. Simple enough, right?
And finally, notice that we've done exactly the same thing here to create a rectangle as we ultimately do in the MouseUp
event handler method. That makes sense, because we just want the instantaneous size of the rectangle while the user is creating it.
Upvotes: 12