And for the undo-feature: in which part of the TPicture are the picture data stored? I mean, which part of the TPicture must I copy and save somewhere, before I add a rectangle, to be able, to do the undo?
Thanks in advance.
I assume you want to create some form of graphic editor, then I would recommend you a few things.
1. Don't work on the picture directly, but work on an intermediate representation, e.g. a TBitmap in your memory, which you then copy into the TPicture to show. The reason for that is, depending on what you load into your TPicture, there can be a 32 bit PNG with alpha channel in there, or a 1 bit BMP pixelmap. If you work directly on the TPicture you need to handle every single format TPicture can display.
Instead do the following. Load the picture into some raw format that is easy to work with, e.g. BMP, TGA, whatever. Then do all your computations on that intermediate format and lastly, when exporting convert it back to the desired format.
2. Do not apply all things to the bitmap directly. This makes things like your undo specifically hard. Instead work with discrete objects, like you see in GIMP or Photoshop (layers!). This also makes undo very efficient. For example you add a rectangle. Shure it's easy to just draw it on the canvas, but let's say you want do do an undo, so you make a snapshot of the picture before and after the rectangle was drawn. If the rect is 10x10 pixels, but your picture is 1000x1000, you just have 99% of your snapshot is wasted memory, as nothing has changed.
If instead you create a new layer for the rectangle, the undo is just removing that layer rather than storing the whole picture twice, you only ever store what's happening to the rectangle layer. Even more efficiently, the layer does not need to be rasterized in the first place, you can store the rectangle layer as an abstract form, similar to how vector graphics work. Then changes to the rectangle also will not need full copies of the pixels of that rectangle, but only a snapshot of it's parameters (x, y, width, hight, color, etc.).
For undo then just put any change as reversible object into a undo class hierachy and maintain a list (or tree) of those objects to go back and forth.
Because I was a bit bored, I attached an example project