# Fillrectangle method

[b][red]This message was edited by Schwarzenegger at 2006-2-11 6:6:23[/red][/b][hr]
Hello dear programmers.
I'm having a problem with this program im writing. (Im trying to create a paint brush like windows has), anyway, I've managed to form a filled rectangle but it can only be drwan from the upper left corner of the "mouse-down" method and to the right or down. If im trying to draw in the other direction, nothing happens - I just get nothing. Now, I've noticed that the rectangle is limited in the X-coordinate to 0 and the same in Y. That means that if the end-point is smaller than the start-point it doesnt respond. I've also come to realize that in the FillRectangle method itself when im opening a brackets () im getting that Im suppose to write the "X-coordinate of the UPPER-LEFT rectangle to fill" - so what does that mean? that I can only draw rectanglr from the upper left corner and down?!! probably not but I just dont know how to solve this problem. (I've pasted some line from my code) so if u know the answer or have a lead please answer me..

Sincirly yours,

Rotem

private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
pictureBox1.Invalidate();

if (flag == true)
{
Graphics G = pictureBox1.CreateGraphics();

if (Rectangle.Checked)
{
if (e.Button == MouseButtons.Left)
{
Point mp1 = new Point(0,0);
mp1.X = e.X;
mp1.Y = e.Y;
f1.PointEnd = mp1;
}
}

• You want to make something like this? http://www.eecs.wsu.edu/paint.net/

First you have to points. Point1, the point where the mouse was pressed, and Point2, the point where the mouse is now. Now you have to calculate the upperleft point of your rect and the size.

[code]
//Point1 is set at Mouse_Down
//Point2 is set at Mouse_Move

// upperleft point, Math.Min will choose the minimal
Left = Math.Min(Point1.X, Point2.X); // X of the point
Top = Math.Min(Point1.Y, Point2.Y); // Y of the point

// size, Math.Abs keeps it positive
Width = Math.Abs(Point1.X - Point2.X];
Height = Math.Abs(Point1.Y - Point2.Y];

FillRectangle(Brush, Left, Top, Width, Height); // fill it
[/code]
• : [b][red]This message was edited by Schwarzenegger at 2006-2-11 6:6:23[/red][/b][hr]
: Hello dear programmers.
: I'm having a problem with this program im writing. (Im trying to create a paint brush like windows has), anyway, I've managed to form a filled rectangle but it can only be drwan from the upper left corner of the "mouse-down" method and to the right or down. If im trying to draw in the other direction, nothing happens - I just get nothing. Now, I've noticed that the rectangle is limited in the X-coordinate to 0 and the same in Y. That means that if the end-point is smaller than the start-point it doesnt respond. I've also come to realize that in the FillRectangle method itself when im opening a brackets () im getting that Im suppose to write the "X-coordinate of the UPPER-LEFT rectangle to fill" - so what does that mean? that I can only draw rectanglr from the upper left corner and down?!! probably not but I just dont know how to solve this problem. (I've pasted some line from my code) so if u know the answer or have a lead please answer me..
:
: Sincirly yours,
:
: Rotem
:
: private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
: {
: pictureBox1.Invalidate();
:
: if (flag == true)
: {
: Graphics G = pictureBox1.CreateGraphics();
:
: if (Rectangle.Checked)
: {
: if (e.Button == MouseButtons.Left)
: {
: Point mp1 = new Point(0,0);
: mp1.X = e.X;
: mp1.Y = e.Y;
: f1.PointEnd = mp1;
: }
: }
:
:
:

Don't forget to do a G.Dispose() when you don't longer need the Graphics object. If you don't, you will be sucking up memory.

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!