四角形を描画する前に元のデータをサーフェスから一時的なビットマップにコピーしてから、ビットマップを元の場所に描画するとうまくいくはずです。
アップデート
すでに受け入れられた回答がありますが、とにかくコードサンプルを共有できると思いました. これは、指定されたコントロールに指定された長方形を赤で描画し、500 ミリ秒後に領域を復元します。
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;
}