Canvas.Invalidate

I use a timer to fire Canvas.Invalidate
In the Canvas.Paint event I pass new parameters and draw a line.

Problem: the previous drawn line gets erased. How to keep previous painted lines?

You will have to
A redraw every thing in your canvas upon invalidate or,
B invalidate only a region of the canvas. (See the LR)

Thanks, but… there’s no other option??

What about an offscreen Picture property you draw data in and in the Paint event, you draw it ?

I have to keep the already drawn lines as the new lines starts where the old one ends… this happens in a 250 ms cycle. Invalidate only a region deletes the previous lines in that region.

like:

The other option is to work on a picture that is affected to the canvas backdrop.

Then it won’t erase anything unless you specifically New it.

I guess that’s the only option

Or you could add your lines to an array (of CurveShape) and draw those as you need to, with the benefit of being able to set properties of each CurveShape.

Regards - Richard.

If you need things to persist then you should

  1. draw to an internal buffer (picture) that you then draw to the screen during the paint event
  2. keep a list of “vectors” or object2d’s that you redraw in the paint event

The graphics you get in the paint event is effectively a “blank canvas” each time so you draw what you need
And in your case that’s “everything that came before”

Anoter simple way to retain what has been drawn is also to DrawInto the canvas in a picture.

@Norman Palardy [quote]If you need things to persist then you should

  1. draw to an internal buffer (picture) that you then draw to the screen during the paint event [/quote]

is it expensive to draw in the paint event something that is going to persist? That is: without drawing to a buffer?

Eventually, yes.
up to 100 lines, its not really an issue. By the time you reach thousands of drawing statements, it will slow down.
It is a problem? No, unless you are writing a game.

All the solutions offered above are correct and work well: I have used them all myself at various times.
Im not sure why there is resistance to them, but for clarity, here are the options

1/ Do it all in Paint Event
When the canvas is invalidated, it will paint. In the paint event, draw all the lines.
This means ‘remembering’ an array of line co-ords

2/ Same, but use a Group2D , containing many Object2D items which are lines. Just add lines as you need to, and in the Paint Event, use g.drawObject myGroup2D

The benefit of this is that you can remove lines at any time, and the refreshed canvas will not show the removed line.

3/ Maintain an offscreen picture - create a picture the same size as the canvas. Draw your lines to that. When the canvas Paint event fires, use g.drawpicture myPicture,0,0 … its very fast and only happens now and then when the canvas needs to be repainted.

One downside of this approach is that it isn’t easy to remove a line: you have to redraw the picture in full to do that.

Jeff, thank you for the detailed explanation.
For the particular case I had in mind (drawing no more than a couple of lines), the first option still seems the easiest solution.