Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,067 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,595 people online right now. Registration is fast and FREE... Join Now!




Invalidate picturebox with rectangle on

 
Reply to this topicStart new topic

Invalidate picturebox with rectangle on

Dumpen
6 Apr, 2008 - 05:51 AM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

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();
        }

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Invalidate Picturebox With Rectangle On
6 Apr, 2008 - 10:56 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
When you call Invalidate() it is going to call the PictureBox's Paint event to do the redraw. This event fires whenever the control needs a repaint (like at the beginning, when resizing etc).

If you wanted you could just reload the picture back into the picturebox as a quick and dirty way of erasing or you could use the Paint event to do your resetting of the picture.

How you want to do this is up to you but that is where you will want to start the journey. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:48PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month