views:

353

answers:

3

I am drawing a rectangle, sleeping for a few milliseconds--then I want to clear the rectangle, but I can't figure out how. (The rectangle is sitting over a graphic so I can't simply cover it up with another rectangle)

                graphics.DrawRectangle(p, innerRectangle)
                System.Threading.Thread.Sleep(75)
                Next I want to clear the rectange...
+4  A: 

You need to redraw the graphic (or at least the part of it under the rectangle). If this is a picture box or something similar, use Invaldiate() to force a repaint.

Jon B
+2  A: 

I guess it should work to copy the original data from the surface into a temporary bitmap before drawing the rectangle, and then draw the bitmap back in place.

Update

There is already an accepted answer, but I thought I could share a code sample anyway. This one draws the given rectangle in red on the given control, and restores the area after 500 ms.

public void ShowRectangleBriefly(Control ctl, Rectangle rect)
{
    Image toRestore = DrawRectangle(ctl, rect);
    ThreadPool.QueueUserWorkItem((WaitCallback)delegate
    {
        Thread.Sleep(500);
        this.Invoke(new Action<Control, Rectangle, Image>(RestoreBackground), ctl, rect, toRestore);
    });
}

private void RestoreBackground(Control ctl, Rectangle rect, Image image)
{
    using (Graphics g = ctl.CreateGraphics())
    {
        g.DrawImage(image, rect.Top, rect.Left, image.Width, image.Height);
    }
    image.Dispose();
}

private Image DrawRectangle(Control ctl, Rectangle rect)
{
    Bitmap tempBmp = new Bitmap(rect.Width + 1, rect.Height + 1);
    using (Graphics g = Graphics.FromImage(tempBmp))
    {
        g.CopyFromScreen(ctl.PointToScreen(new Point(rect.Top, rect.Left)), new Point(0, 0), tempBmp.Size);
    }

    using (Graphics g = this.CreateGraphics())
    {
        g.DrawRectangle(Pens.Red, rect);
    }
    return tempBmp;
}
Fredrik Mörk
+2  A: 

If the rectangle is sitting completely over the graphic you should be able to just redraw or refresh the underlying graphic. If it is not, you will need to redraw the rectangle using the background color, and then refresh the underlying graphic.

Robert Harvey