Rectangle in a pictureBox

E

emferrari

Hi all

I have a picturebox which I will have a image loaded to it and some
text fields with coordinates. I am drawing a rectangle according to the
coordinates in the text fields.

Rectangle r = new Rectangle(int.Parse(this.txt_Left.Text),
int.Parse(this.txt_Top.Text), ControlWidth, ControlHeight);
pictureBox2.CreateGraphics().DrawRectangle(new Pen(Color.Red, 1), r);

This is working fine, draw the red rectangle. But I want to clear the
rectangle when I change the coordinates. Does somebody knows how to do
that?

Thanks
 
M

Markus Stoeger

Hi all

I have a picturebox which I will have a image loaded to it and some
text fields with coordinates. I am drawing a rectangle according to the
coordinates in the text fields.

Rectangle r = new Rectangle(int.Parse(this.txt_Left.Text),
int.Parse(this.txt_Top.Text), ControlWidth, ControlHeight);
pictureBox2.CreateGraphics().DrawRectangle(new Pen(Color.Red, 1), r);

This is working fine, draw the red rectangle. But I want to clear the
rectangle when I change the coordinates. Does somebody knows how to do
that?

Why don't you use a class inherited from Control and override OnPaint to
do all the drawing? You can use the DrawImage function to draw the image
that the PictureBox draws right now. And use DrawRectable as above.
This will also make sure that your coordinate rectangle will get cleared
every time OnPaint gets called.

Max
 
M

Markus Stoeger

I forgot.. Pens should be disposed (they consume GDI handles):

using (Pen pen = new Pen(whatever)) {
graphics.DrawRectangle(pen, ...);
}

Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top