By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Rectangle in a pictureBox

P: n/a
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

Jun 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
em*******@gmail.com wrote:
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
Jun 16 '06 #2

P: n/a
Markus Stoeger wrote:
em*******@gmail.com wrote:
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);


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

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

Max
Jun 16 '06 #3

P: n/a
All right. I'll give a shot and let you know!

Thanks!

Markus Stoeger wrote:
Markus Stoeger wrote:
em*******@gmail.com wrote:
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);


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

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

Max


Jun 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.