Save Canvas contents into a picture after Paint Event completed

I have a canvas that has a view of my model.
I want the last thing the paint event do is to save a picture of the canvas.

I’ve seen a thread about saving as PDF but I’m finding it difficult to follow that thread.

I found this in the documentation but I certainly can’t call it from my Paint Event…

Dim p As New Picture(Me.Width, Me.Height)
Me.DrawInto(p.Graphics, 0, 0)
Canvas1.Backdrop = p

P.S. TIA

PaintEvent(g as graphics)
    DrawMe(g as graphics)

Your own Method, you could subclass your canvas to add a method and properties there.
DrawMe(g as graphics)

at any place outside of paint event

Dim p As New Picture(Me.Width, Me.Height)
DrawMe(p.Graphics)

Save your Picture

2 Likes

OFF: I really wish a listbox with owner-drawn content would work with DrawInto.
I just get a frame with a heading.

You need a method that will draw the view of your model into a graphics object. Then you either call it with the graphics from the paint event, or the graphics from the picture. Separate them in your thinking. They are totally separate, even though they produce the exact same result. There is no reason to use DrawInto for this.

2 Likes

Hello,
draw intro delivers a simple quality
draw outside of the paint routine
into an image and draw it in Paint
a small example of this

https://www.dropbox.com/s/v15c4vtlnvv5def/Test-Picture-externPaint.xojo_binary_project?dl=1

This is actually quite easy. What you need to do is create a picture buffer at the top of the paint event, draw onto that and then draw the picture onto the canvas. That way the picture is always up to date.

In the Paint event:

// at the top
Dim gg as Graphics = g
Dim p as new picture(g.width,g.height)
G= p.graphics

... drawing code here ...

// at the bottom 
gg.drawpicrure p, 0, 0

Then if you want it stored somewhere, save it. I don’t suggest doing it every time, but you could set a flag and then call refresh:

If savepic then
    p.save(placeToSave, picture.formats.png)
    Savepic = false
End if
1 Like

Ya… That’s a good idea.
My paint now looks like:

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
  Draw(g)
End Sub

And if i want to draw into a bitmap image:

Dim p As New Picture(512, 512)
Draw(p.Graphics)

I assume there is a way to print the bitmap and then have it saved as pdf by the OS printer drivers.

1 Like

Or just use the PDFDocument class.

2 Likes

Please do not create pictures in the paint event. You’re not in control of when the canvas is asked to be painted, it can be called +100s of times if the user resizes the window. The paint event (and any code it calls), needs to be as simple and light as possible.

Please follow @MarkusR method. Yes @MarkusR method will add the overhead of a function call everytime the OS asks the canvas to update, but it should take way less time than creating and releasing a picture.

@MarkusR method also allows you to use the same drawing code to pipe to a printer, PDF or a high resolution image.

1 Like