|
-
April 6th, 2008, 08:48 AM
#1
Invalidate picturebox with rectangle on
Hello.
I have a simple picturebox where there is a image loaded in
Im drawing a rectangle on the picturebox and when I press a button I want to invalidate it (remove the rectangle), but it doesnt seem to work
The rectangle is drawn fine and the image loads up to, but when I press my button nothing happens
Here is the code im using:
Code:
private void drawOnPic()
{
// Attach grapich to picturebox
Graphics g = Graphics.FromImage(pictureBox1.Image);
// Create a new pen that we shall use for drawing the line
Pen PenStyle = new Pen(Color.Red, 1);
// Draw a 50x50 pixels rectangle (x, y, width, hight)
g.DrawRectangle(PenStyle, 20, 20, 50, 50);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
drawOnPic();
}
-
April 6th, 2008, 11:34 AM
#2
Re: Invalidate picturebox with rectangle on
Hey Dumpen,
I haven't really used the System.Drawing stuff very much....but here's what I found.
When I copied and pasted your code, the rectangle never showed up. To get the rectangle to show up I changed two things. I changed the graphics object, and I had to move the call to the draw method outside of the form's load event (I moved it to a button for simplicity).
After I changed that I think it functioned the way you were expecting. One button would draw the rectangle and the other would invalidate it causing it to repaint without the rectangle.
Code:
private void drawOnPic()
{
// Attach grapich to picturebox
Graphics g = pictureBox1.CreateGraphics(); //I changed this.
// Create a new pen that we shall use for drawing the line
Pen PenStyle = new Pen(Color.Red, 1);
// Draw a 50x50 pixels rectangle (x, y, width, hight)
g.DrawRectangle(PenStyle, 20, 20, 50, 50);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void button2_Click(object sender, EventArgs e)
{
drawOnPic(); //I moved the call to this method from the form's load to this button's eventhandler.
}
private void Form1_Load(object sender, EventArgs e)
{ }
Someone else who actually knows what they're doing can probably help a lot more, I'm just shooting in the dark!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|