Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalidate picturebox with rectangle on

Status
Not open for further replies.

leprogrammer

Programmer
Mar 21, 2008
25
DK
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();
        }
 
Where are you capturing the paint event?

You need to click on your picturebox in the designer, switch to the action/event view (the lightning bolt above properties) and double click on the Paint event.

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
drawOnPic();
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top