I have a TImage bitmap on a form, and a TPaintBox with the same dimensions. The user draws some polylines on the paintbox canvas. Using paintbox means they can "erase" a line if needed and the backgound stays visible.
What I see on the screen is the background image with the lines drawn on top - good! When the user is happy, I need to store the result (to a file) which includes everything I see on the screen (f.e. background and polylines).

So how do I make the composite image to save a file - combining the image canvas and paintbox canvases somehow?
Or must I store all the drawing commands and "replay them" on the image canvas?

4 Replies 4

You are already storing the needed information that allows you to draw the user lines in OnPaint even of the TPaintBox right?

So before storing the image just draw those lines to the bitmap the same way as you draw then in OnPaint event of TPaintBox and you will get the combined image.

Combining the two canvases directly might not work. I'm not sure if Paint Box canvas transparency would be properly preserved.

@SilverWarrior
Yes, I do have an array of TPoints for the straight lines (Later I need rounded "rubber-band line"), so
when the user is happy I draw the lines on the backgound TImage (which is stretched to fit the form).
All looks good on the screen. Save to file.
Problem with the saved bitmap file - it is "original" size, not the larger displayed size - so the drawn lines are in the wrong place! I guess I will have to avoid stretching and obtain an image that is the exact size of the form!
Regarding Paintbox canvas, I also wondered if the PaintBox canvas is actually "transparent" like it appears. If it IS, then some sort of operation (maybe bitblt??) that would combine ANY drawing with background. This would be better for freehand/shapes other than straight lines! I guess otherwise a "pixel by pixel"

If you are showing scaled image as background then you will have to adjust X and Y position of your points to keep the appropriate relative position to the original image.

TransformedXPos := OriginalXPos * (Image1.Width / Bitmap.Width);
TransformedYPos := OriginalYPos * (Image1.Height / Bitmap.Height);

Also since your original image is shown scaled it will probably be better to redraw the lines on the original bitmap instead of stretch drawing of Paint Box canvas since stretching of canvas contents would also stretch the lines so they could end up looking blocky.

The biggest problem will be transferring freehand drawing.

If you really need freehand drawing on scaled image you might consider using some 3rd party image editing library. Doing so you may get access to some other useful image manipulation features.

@SilverWarrior, thanks for reply, and good points. I shall try and get backgound images in the correct size and avoid scaling for the reasons you describe!

Drawing manipuation...

(Background, this is for dawing a sailing "race course" around a set of "Mark" shapes (circles) - user clicks each mark of the course in turn. a Polyline stores vertices at centre of each Mark. (circular shape) in turn. So far, so good! SO have a set of straight lines vertices on each mark Shape.

  • But course "rules" are do NOT sail over a mark but sail round it either clockwise or anticlockwise

  • User can click each Mark to change color Red/Green to indicate.

  • So next, I would like to replace each straight-line vertex with a smooth transition (arc or ellipse). going round each Mark in the correct direction (leave Mark to either Left or Right.

Maybe have to work out the angle of each of the two lines at a Mark, and then work out how to describe the Arc! Seems tricky! Is that a valid approach do you think? any ideas to simplify?

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.