How to erase one image from TCanvas?

I have a transparent panel in my form that contains a Canvas (I think its implementation is not very important here, someone tells me otherwise). I am drawing a lot of bitmaps on this canvas using:

Panel.Canvas.Draw(Image.Left, Image.Top, Image.Picture.Bitmap, Opacity);
//Image is a TImage instance
//Opacity is an integer 0..255

      

But when I make some changes to the TImage, such as redistributing or moving the form, I fix its image in the panel. The problem here is that the old positioned image stays on the canvas. Now my question is, is there a way to erase one bitmap on the canvas? If so, what would it be like? If not, is there a way to delete all the contents of the canvas? This way I could redraw the remaining images.

Edit:

In the end, my problem was on the transparent panel I was using. His method Paint

was magenta and did not nullify his canvas to erase the controls on it. The problem is solved, anyway. What should I do with the question?

-4


source to share


2 answers


Not really, because the canvas doesn't track different images.

However, you can easily "clean up" any part of your canvas by simply painting it.
This will allow you to redraw all other images.

However, this can be time-consuming if there are many photos and the user is busy dragging and dropping one image around; as this will rewind multiple times for every small change in position of the moving picture.

One option is to paint the "active" image using an XOR mask while adjusting it. It can then be erased by simply redrawing at the same position using an XOR mask. This has the disadvantage that colors are distorted but very effective.

Another option is to make a copy of the part of the canvas on which you are going to paint the active picture before painting. Then you have a simple mechanism to erase the new image by redrawing the copy in the correct position.


Edit: Detailed explanation of the last option in response to a comment from the guild:

And how could I draw it since I have no background? Do you have some kind of transparent brush?

Let's say you want to draw and move an image (maybe a 20x60 blue rectangle):



  • Let's say you start with a blank canvas, background clWhite

    .
  • Starting position (25.75), so:
  • (A) First copy the 20x60 by (25.75) rectangle onto the canvas.
  • The copy will of course be completely white, but that's exactly what your background looks like.
  • Now draw a rectangle at this position.

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

  • Draw your copied image (25.75). NB Lack of transparency! You want to restore the Canvas to state (A) before you draw the blue rectangle.
  • Copy the 20x60 Rect to (40.90). (Again, it will be completely white).
  • Draw a blue rectangle at (40.90).

Ok, so far so good, but our copies are always white. So add a second rectangle. (This time red and 80x10.)

  • We will 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 that it overlaps the blue.
  • (B) Copy 80x10 Rect to (45.95)
  • Notice that this time, part of the copy is blue and the rest is white.
  • Now let's draw a red rectangle at (45.95)

As a final step, reduce the size of the red rectangle to 5x5:

  • Draw the copied image (45.95). NB . Again it is very important that we do not use transparency, because we are trying to restore the part of the image where we drew the red rectangle so that it looks like it did in (B) .
  • Copy 5x5 Rect to (45.95)
  • Draw a smaller red rectangle.

This is a simple re-rinsing process. As it happens, this is the same method that Windows uses to draw the mouse cursor as it moves.

Side note . If the image you are drawing has the wrong shape, it doesn't matter. If your fallback rectangle completely covers the image you are drawing, this method works.

+4


source


Your whole approach to painting is wrong. Windows paint surfaces do not remember their contents. Drawing design in Windows requires each window to be able to re-draw itself when asked, that is, when a message is sent to it WM_PAINT

. You must answer by WM_PAINT

drawing what you are asked to draw. You are breaking this rule. You will need to redesign your program to fit into the system.

Follow these steps:

  • Add TPaintBox

    to your panel.
  • Add an event handler OnPaint

    to the paint box that does the whole picture.
  • When you need to force repaint, call Invalidate

    in the paint box.


What is it. Note not mentioned here WM_PAINT

. This is because the VCL wraps it all up for you and presents it in a more digestible way.

The essential reading on this (and many more) is Petzold's classic topic, Windows programming.

+3


source







All Articles