Graphics -> Picture?


Playing around with something here, I want to take a “screenshot” of a container control and copy it to a picture object.

Thinking along the lines of using containercontrol.drawinto to populate a graphics object with the image then try to convert the graphics object into a picture - somehow! Not really sure if this is really possible or best practice…

Any advice/pointers before I go too deep here?


Create a ContainerControl and add the paint event:

Sub Paint(g As Graphics, areas() As REALbasic.Rect) g.ForeColor = RGB(0, 151, 255) g.DrawOval(0, 0, g.Width, g.Height) End Sub

Put an instance of ContainerControl1 onto Window1.

In some event of Window1 (for example Close) put this code:

Dim pic As New Picture(ContainerControl1.Width, ContainerControl1.Height, 32) ContainerControl1.DrawInto(pic.Graphics, 0, 0) BREAK
Now when you close the window, you can click on the pic instance in the debugger and check its content (a blue circle).

I did a little test with DawInto - but the result seems to have poor quality…

I put a container MyContainer and a canvas MyCanvas on a
window and a pushbutton with this in its action event:-

[code] dim pic as new picture( self.myContainer.Width, self.myContainer.Height )
dim g as graphics

g =
self.myContainer.DrawInto( g, 0, 0 )

self.myCanvas.Backdrop = pic

That’s awesome - thanks guys - much appreciated.
Saved me some messing about…

Run into a bit of an issue.

When working with an array of containers I get an error on the following code

[code]Sub Constructor(Containers() As ContainerControl)
// We need to build the container all through code!
For i As Integer = 0 To Containers.Ubound
Dim pic As New Picture (Containers(i).Width, Containers(i).Height)
Dim gr As Graphics

gr =
Containers(i).DrawInto (gr, 0, 0)

// Canvas1.Backdrop = pic


End Sub
I get the following message on the DrawInto statement - lots of playing around hasn’t sorted it unfortunately.

The method is protected.  It can only be called from within its class

Tried outside the constructor and all sorts. Could one of the Xojo bods tell me if this is a bug or if I’m missing something? Couldn’t find anything in Feedback


You cannot use DrawInto outside of the container control itself, or the window instance owning the container control instance, or another control in the window owning the container control instance.

ok - thanks

Went for a walk and thought of a better approach - Onwards…