Tom
Tom

Reputation: 6707

How to draw over a fresh copy of Timage with Canvas.CopyRect()

I have a code to draw a moving line over timage, and I want the previous old line always to disappear, but this code makes all lines to appears there for ever... When I enable DoubleBuffering to form, the code below works OK but the background bitmap located in myimg does not show at all.

  Fbitmap.Canvas.Pen.Color:=clRed;
  Fbitmap.Canvas.Pen.Width:=2;
  Fbitmap.Canvas.MoveTo(Xo,Yo);
  Fbitmap.Canvas.LineTo(Xs,Ys);
  myimg.Canvas.CopyRect(Rect(0, 0, Width, Height), FBitmap.Canvas, Rect(0, 0, Width, Height));

Upvotes: 0

Views: 2216

Answers (2)

adlabac
adlabac

Reputation: 416

There are several ways you can accomplish this. The easiest one is to leave the image as is, and to add another component (TShape) which you will move over the image.

If you have to write the line on the image, you need to keet the original image in the memory and use CopyRect over the one with the line before drawing a new one. Faster variation of this method is to keep in memory only the part of the image where the line will be drawn, so you can copy it over the line later, thus deleting it.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613511

It's not surprising that this happens. TImage is intended for static images. When you draw on its canvas, what you draw stays there. That is by design.

It seems to me that you have chosen the wrong control. Obvious candidates for this are:

  • A TPaintBox. In the OnPaint handler draw the background, and then the lines.
  • A custom control that draws the background in response to WM_ERASEBKGND, and the foreground in response to WM_PAINT.

The latter option may be overkill for you but in my experience is the best defence against flickering.

Upvotes: 6

Related Questions