[Solved] How can erase a single Picture from a TCanvas?


Not really, because the canvas doesn’t keep track of distinct images.

However, you can easily “clear” any part of your canvas simply by drawing over it.
This will allow you to redraw all remaining pictures.

This could however be quite time consuming if there are many pictures and a user is busy dragging a single picture around; as this would result in a lot of redrawing for each slight position change of the moving image.

One option is to draw the “active” image with an XOR mask while it’s being adjusted. Then it can be erased by simply redrawing in the same position with an XOR mask again. This has the disadvantage that colours become distorted, but it is very efficient.

Another option is to make a copy of part of the canvas where you intend drawing the active picture before you draw. Then you have a simple mechanism to erase the new picture by redrawing the copy in the correct position.


Edit: Detailed explanation of last option in response to comment from Guill:

And how could I draw over it since I have no background? There is some kind of Transparent brush?

Suppose you want to draw and move a picture (perhaps a blue rectangle 20×60):

  • Let’s assume you start with a blank Canvas, clWhite background.
  • The initial position is (25,75), so:
  • (A) First copy the 20×60 Rect at (25,75) on your Canvas.
  • The copy will of course be entirely white, but that is exactly what your background looked like.
  • Now draw your rectangle at that position.

Cool, first bit done. now you want to move the rectangle to (40,90):

  • Draw your copied image at (25,75). NB No transparency at all! You want to restore your Canvas to the state at (A), before you drew the blue rectangle.
  • Copy the 20×60 Rect at (40,90). (Again it will be entirely White)
  • Draw your blue rectangle at (40,90).

Ok, so far so good, but our copies are always White. So let’s add a second rectangle. (This time red and 80×10.)

  • We’ll discard our current copy because we no longer want to move the blue rectangle.
  • We want to place the red rectangle at (45,95), so it overlaps the blue.
  • (B) Copy the 80×10 Rect at (45,95)
  • Note, this time part of the copy is blue, the rest is white.
  • Now draw the red rectangle at (45,95)

As a final step, lets decrease the size of the red rectangle to 5×5:

  • Draw your copied image (45,95). NB Again It’s very important that we do not use transparency, because we’re trying to restore the portion of the image where we drew the red rectangle back to what it looked like in (B).
  • Copy the 5×5 Rect at (45,95)
  • Draw the smaller red rectangle.

This is a simple rinse-repeat process. As it so happens, this is the same technique used by Windows to draw your mouse cursor as it moves.

Side note: If the image you’re drawing is an irregular shape, it doesn’t matter. Provided your backup rectangle fully covers the image you’re drawing, this technique works.

4

solved How can erase a single Picture from a TCanvas?