In VS 6.0 and in OpenGL, this is fairly simple, you just use the XOR
drawing mode, which draws a line normally the first time, and then
"erases" it the second time.
GDI+ doesn't have support for the pen modes anymore, but your task is
not too much more difficult.
There are a couple ways to do this:
1. you could keep a bitmap of what the clear, or erased state looks
like, and whenever you want to erase the line, you would do something like:
//...
private Image savedImage;
//...
// your standard paint method
private void My_Paint(object sender,
System.Windows. Forms.PaintEven tArgs e)
{
if (something_has_ changed_like_th e_form_has_been _resized)
{
Rectangle rect = this.GetDrawing Rectangle();
this.savedImage = new Bitmap(rect.Wid th, rect.Height);
Graphics gx = Graphics.FromIm age(savedImage) ;
gx.Clear(this.B ackColor);
// draw the normal, erased state
// ...
gx.Dispose();
}
Graphics g = e.Graphics;
g.DrawImageUnsc aled(this.saved Image,0,0);
// now draw any of the lines, pictures, etc.
// that you want to be erased.
}
2. The previous method is good if you need to completely redraw every
element of your picture each time, but what if you have a bunch of lines
already drawn, and you only want to erase one of them? Here's where you
can use a TextureBrush as a sort of eraser.
//...
private Image lastImage;
private Brush eraserBrush;
//...
// your standard paint method
private void My_Paint(object sender,
System.Windows. Forms.PaintEven tArgs e)
{
if (something_has_ changed_like_th e_form_has_been _resized)
{
// we may need to redraw everything...
Rectangle rect = this.GetDrawing Rectangle();
this.lastImage = new Bitmap(rect.Wid th, rect.Height);
Graphics gx = Graphics.FromIm age(this.lastIm age);
gx.Clear(this.B ackColor);
// draw the normal, erased state
// ...
// draw all of the elements that should be drawn...
gx.Dispose();
this.eraserBrus h = new TextureBrush(th is.lastImage);
}
// all of our lines, pictures, etc. are already stored
// inside lastImage, so we don't need to do anything here.
// let's say we kept track of some coordinates for a line,
// and also kept track of whether or not we need to do
// some erasing
if (bLineNeedsEras ing)
{
// by drawing to this Graphics object, we're updating the
// bitmap image.
Graphics imageGraphics = Graphics.FromIm age(this.lastIm age);
// assuming the first line was drawn with a width of 1 (float)
Pen p = new Pen(this.eraser Brush, 1F);
// assuming we kept track of a couple System.Drawing. PointF
// points representing the ends of the line
imageGraphics.D rawLine(p, this.linePoint1 , this.linePoint2 );
imageGraphics.D ispose();
}
Graphics g = e.Graphics;
g.DrawImageUnsc aled(this.lastI mage,0,0);
}
This second way is almost always the better way to go.
One last note: you're going to notice the screen flickering if you're
doing any kind of drawing. To alleviate this, look up the SetStyle
method, and use something like:
this.SetStyle(C ontrolStyles.Us erPaint, true);
this.SetStyle(C ontrolStyles.Do ubleBuffer, true);
this.SetStyle(C ontrolStyles.Al lPaintingInWmPa int, true);
in your own initialization code.
Tomomichi Amano wrote:
Hello
How can I delete (clear) lines that were made useing Graphics.DrawLi ne() ?
Thanks in advance! Have a nice day!