How to erase a line

Using DrawLine of a Graphics object, we can draw line. But how to erase a line?
[79 byte] By [visharad] at [2007-11-20 10:07:49]
# 1 Re: How to erase a line
HI

One option is chage line color to background control color.

Regards
Ravi.Battula
battula32 at 2007-11-9 11:34:53 >
# 2 Re: How to erase a line
Yes, I do that in one of my programs too. Just one thing, if you were to make the eraser work like any other eraser ( erases while dragging the mouse ), you would have to make a Brush object, which is the same colour as the current backcolor. Another thing is the issue of erasing a line ( or any other object ), which is drawn accross multiple colours, use the object ( picturebox's, or Form's ) Backcolor then
HanneSThEGreaT at 2007-11-9 11:35:53 >
# 3 Re: How to erase a line
I am drawing the line using Graphics object and Pen object. Pen object is being passed as a parameter in DrawLine of Graphics Object. I get Graphics object by calling CreateGraphics of Form object.

When I move mouse, then I want line to change i.e. previous line should be erased and new line should be drawn.
To erase previous line, I draw that line using a Pen of same color as Form's back color. But that does not work.

Do I need to use Brush object instead of Pen object?
visharad at 2007-11-9 11:37:03 >
# 4 Re: How to erase a line
No, I was just trying to make my point. You can use a Pen object, but it must be the same size as the one you want to erase.
Can you post the code you've used
HanneSThEGreaT at 2007-11-9 11:37:58 >
# 5 Re: How to erase a line
//Note:- frmAuthor is the form on which lines are drawn.
//Following is the code inside the function that draws line

Point pt1, pt2;
if(x1 != 0)//this condition means a previous line exists.
{
//Erase previous line
pt1 = new Point(x1, y1);
pt2 = new Point(x2, y2);

//Erase line by drawing line of color as form's back color.
Pen PenToErase = new Pen(frmAuthor.BackColor, 2);
gr.DrawLine(PenToErase, pt1, pt2);
gr.Flush();
//I did not know if gr.Flush will help. I tried both with and
//without it.
//gr is declared as Graphics object as class member. It has
//already been got in another function by calling
//frmAuthor.CreateGraphics()
}
//
//Here the code to calculate new values of x1,y1,x2,y2 is written.
//
pt1 = new Point(x1, y1);
pt2 = new Point(x2, y2);

//Draw line
Pen blackPen = new Pen(Color.Black, 2);
gr.DrawLine(blackPen, pt1, pt2);

The line is drawn as left mouse button is held down and mouse is moved. I want only one line to be on screen because previous line should be erased. But the previous lines also remain because of which instead of a black line, there is black area.
visharad at 2007-11-9 11:38:57 >
# 6 Re: How to erase a line
Did I read correctly, you want only one line to appear at a time Is that right
Well, if that is indeed the case, why not simply just use the Graphic ( gr ) object's Clear method, and just specify the Form's Backcolor, something like :
gr.Clear(frmAuthor.BackColor)
HanneSThEGreaT at 2007-11-9 11:39:58 >
# 7 Re: How to erase a line
You don't erase the line at all. You simply don't draw it again. If done properly, which you are not, then GDI+ drawing gets done ONLY in or from the Paint event handler of the control you're drawing on. You are supposed to set the values of control variables and then read those control variables in the Paint event handler and do the drawing. If you want to change the drawing you simply change the values of the control variables and then force a repaint. Here's how you would draw one line and one line only on a form. Create a new project and add the following code, then run the project and start clicking and dragging.'These are the control variables, i.e. they control how and what gets drawn.
Private start As Point = Point.Empty
Private [end] As Point = Point.Empty
Private draw As Boolean = False

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'Don't draw while dragging.
Me.draw = False

'Clear the old line.
Me.RefreshLineArea()

'Record the starting point of the new line.
Me.start = e.Location
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
'Record the ending point of the new line.
Me.end = e.Location

'Start drawing again when dragging stops.
Me.draw = True

'Draw the new line.
Me.RefreshLineArea()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Me.draw Then
'Draw the current line.
e.Graphics.DrawLine(Pens.Black, Me.start, Me.end)
End If
End Sub

Private Sub RefreshLineArea()
'Invalidate the smallest rectangle that contains the current line.
Me.Invalidate(New Rectangle(Math.Min(Me.start.X, Me.end.X), _
Math.Min(Me.start.Y, Me.end.Y), _
Math.Abs(Me.start.X - Me.end.X), _
Math.Abs(Me.start.Y - Me.end.Y)))

'Force the form to repaint.
Me.Update()
End SubVoila!
jmcilhinney at 2007-11-9 11:41:02 >