How to clone a Graphics ?

Hi ,

I want to make some drawings test (before printing) in a graphics.
but for that, I need to have a copy of the printing graphics object (not to destroy the original graphics where there are already some printings)

how can I have a clone of a given (printing) graphics object ?

thanks.

As far as i’m aware you can’t.

I didn’t understand what you are trying to achieve but could you create a picture and draw it that?

Print it to a new Picture and do the testing drawings there ?

I want to test the real printing height of a textarea, before printing it.
so I loop using styledtextprinter.drawblocks until I don’t have a styledtextprinter.eof

as I’m printing the rest of the page, I want to have a graphic to test that has the same caracteristics as the original one, but that
don’t mix the original one.

Have you thought about just using a Picture as the clone? Once you have the Graphics object for the printer, you can use its dimensions and resolution to make a picture and treat its Graphics object as if it were a clone. Just instead of NextPage, you call ClearRect.

I have the same question, but for a slightly different purpose:

I have loaded an image from a file, into a Picture object. Now I like to add a Watermark to it. As I cannot directly draw into the loaded picture (its Graphics property is nil), I have to clone it first.

But when I simply do this:

dim clone as new Picture (orig.Width, orig.Height) clone.Graphics.DrawPicture orig, 0, 0

… then I end up with a bad copy of the picture if it’s not using the Mac’s default resolution (72 dpi). That is, on Windows, where the dpi is usually 96 instead of 72, this leads to wrongly sized files when I save or draw the clone.

So I tried this:

dim clone as new Picture (orig.Width, orig.Height) clone.HorizontalResolution = orig.HorizontalResolution clone.VerticalResolution = orig.VerticalResolution clone.Graphics.DrawPicture orig, 0, 0

However, that does not help at all. Same results.

The way to make it work was rather counter-intuitive. I had to change the original picture’s dpi to 72, then clone it, then restore its original dpi:

dim oh as integer = orig.HorizontalResolution dim ov as integer = orig.VerticalResolution orig.HorizontalResolution = 72 orig.VerticalResolution = 72 dim clone as new Picture (orig.Width, orig.Height) clone.HorizontalResolution = orig.HorizontalResolution clone.VerticalResolution = orig.VerticalResolution clone.Graphics.DrawPicture orig, 0, 0 orig.HorizontalResolution = oh orig.VerticalResolution = ov clone.HorizontalResolution = oh clone.VerticalResolution = ov

Is that really the indended behavior? Or is this a bug? It happens the same wrong way on Mac and Windows unless I do what’s shown in the last code example above.

I’d expect that the docs would provide an example for how to properly clone a Picture that I can modify but I could not find one.

Have you tried CopyColorChannels & CopyMask ?

@Norman Palardy - that indeed works better. It preserves the dpi information. Sadly, for a program that watermarks a jpeg file and then saves it back to disk this is not always sufficient because we’d lose EXIF information, as the Picture object probably does not preserve that. Regardless, this is a good solution to clone a picture. Thanks.

Yeah you’d have to read those tags out, clone it, watermark it and then put those tags back into the saved image somehow