Search code examples
delphicanvaslinedrawerase

Erase or delete a line drawn on a TImage Canvas


this is my first question in stackoverflow , i've searched everywhere a cross the web before posting here,so you guys are my last chance

i'm making a little program in Delphi xe5 which consist in drawing lines on a football pitch(TImage) using Canvas and MoveTo(X,Y) method, everything works great

program picture

but my problem is that i want to erase the eralier line just before drawing the next one so i cannnot have two lines at same time, how can i do that ?

this is a snippet of code i'm using for drawing the lines :

 procedure TForm2.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

 const
Line: Integer = 0;
 begin

if Line = 0 then begin
   Canvas.MoveTo(X,Y);
   Line := 1;
   Label1.Caption := IntToStr(x) ;
   label2.Caption :=  IntToStr(y);
End
else if Line = 1 then begin
   Canvas.LineTo(X,Y);
   Line := 0;
   Label3.Caption := IntToStr(x) ;
   label4.Caption :=  IntToStr(y);

end;
end;

Solution

  • If you do not have areas filled with color, you can do all drawing with Pen.Mode set to pmXOR. It will give some odd points where lines cross (for example where the red line crosses the blue circle), but when you re-draw the red line - it will disappear.

    Just add:

    Canvas.Pen.Mode := pmXOR;
    

    If necessary - remember the existing Pen.Mode and restore it when finish drawing.