0

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:

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();
        }
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by CarlosAMerighe
0

You need to invalidate before you create your image, this insures your event is fired.

0

In the button Click event, call the Load() method. Set the image's ImageLocation property first.

For example, in my form Load event, I load the picture box as follows:

pictureBox1.ImageLocation = @"E:\Test\Photo1.jpg";
pictureBox1.Load();

In the button click event, I do this:

pictureBox1.Load();

I hope it helps.

Carlos A Merighe.

Edited by CarlosAMerighe: Fixing code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.